cakePHP 2.1 New Extending Views Feature
cakePHP 2.1 New Extending Views Feature –
CakePHP Is awesome Framework.I Love working on cakePHP.Few Days back I migrated from cakePHP 1. to the New 2.1 stable Version of the CakePHP .CakePHP 2.1 Comes With inbuilt awesome Features.It makes Work more rapid,The Best New feature according to me is Extending views,This Extending the Views From a Parent View or Common template is an Awesome and Revolutionary CakePHP Feature.
So Today I m Gonna Write About this New Extending Views Feature of the CakePHP and put a nice Tutorial How to Use Extending Views Feature of the cakePHP.
cakePHP 2.1 New Extending Views Feature Tutorial –
Before Moving in to Extending Views Tutorial we’ll see some Basics.Like What is CakePHP Extending Views Feature?How to make Common Template or Parent View for Extending Views? Lets Move on and See.
What Is cakePHP Extending Views Feature ?
cakePHP’s Extending View’s Main Motive is DRY(Don’t Repeat Yourself).You can Create a Template View or a Common View And Inherit in all your views.Idea is to Wrap a view in another.So that you should not repeat same markup again and again.
How to Use cake’s Extending Views Feature –
To be able to use cake’s Extending View Feature We gonna Create a Common view ,which Will act as parent view for all other views which may use same mark up as parent.So now you will only have to create view’s layout once,then make it parent .Then just extend it to all your views.
The View Template or Parent View(Extending View)–
The main View Template .Used By all the Views.This is simple parent template which will take sidebar ,content and title from child view.by default if in child view we do not set ‘title’ and ‘sidebar’ in child then all content will be available in content variable.
<h1><?php echo $this->fetch(’title’); ?></h1>
<?php echo $this->fetch(’content’); ?>
<?php echo $this->fetch(’sidebar’); ?>
The Child View Or Extender of parent-
The Child View Which Uses The Parent OR view Template.WE’ll set the variable that parent needed ,After setting variables the rest of the child view body will go in cake’s content (‘content variable’).So its quite easy,we just need to define here sidebar start to end and title variable in this case.The rest body will automatically put in content variable by cake.
$this->extend(’/Common/view’); //Extending the Parent View
$this->assign(’title’, $post); //Sending title variable to Parent Template
$this->start(’sidebar’);//start of sidebar content
echo $this->Html->link(’edit’, array(
’action’ => ’edit’,
<?php $this->end(); //End Of the SIdebar Content ?>
// The remaining content will be available as the ’content’ block
// in the parent view.
Now this Child View have layout of the Parent view.You do not have to reuse the code.
The new Feature is Awesome in cake and Make Framework Produce more reusable Code and Increase the Speed of Web application development.I have Enjoyed working in cake and migrated to cake 2 Due to this awesome New Extending Views Feature.I hope you liked this feature too.Keep visiting.