I read this and this.
But i couldn’t find the answer.
I have this file, where my action’s views exist:
View/MyController/index.ctp
I also have this file
View/Commons/blocks.ctp
Blocks.ctp file includes these:
$this->start('sidebar1');
echo 'Some content';
$this->end();
$this->start('sidebar2');
echo 'Some content more';
$this->end();
So in the “index.ctp” file i want to fetch sidebar1 or sidebar2.
How can i do this?
I wrote this to index.ctp, but didn’t work.
<?php echo $this->fetch('sidebar1'); ?>
Also this one didn’t work
<?php echo $this->fetch('../View/Commons/blocks.ctp/sidebar1'); ?>
Thank you
Put at top of your index.ctp:
With blocks and view inheritence, you can create “sub layouts” which are basically analogous to the standard Cake layout file. So you’d have the main layout.ctp, and the controller-action view ctp would be based on a parent view file (e.g. /Commons/xxxx.ctp) which is “populated” via blocks.
Blocks are like elements but less “formal” unless you use the “view inheritance” features. Their markup & data get created in your scripts, in possibly multiple locations so they can be more cumbersome to debug/maintain (i.e. imagine appending markup to a block from multiple classes). They’re also harder to reuse if you don’t use inheritance.
Elements are more like stand-alone view files that can be used within any controller+action view or layout: all the markup is in one place and you just pass in the data.
Bottom line: if you’re new to Cake, you can get by fine with just elements. View inheritance can help make views/layouts more elegant but at the price of some complexity.