Re: Difference between Blocks and templates
First, we should probably start by using the terminology that is used by CakePHP. We have layouts, views, elements and blocks. Layouts, views and elements all use CakePHP templates, as denoted by the extension "ctp".
As a result, blocks complement templates, whether it be layouts, views or elements. You could use a block in any of those.
The best example of block usage is the script, css and content blocks as used by CakePHP itself. All output from the view is written to the content block. All css links that are not inline are written to the css block (and this can be done from the layout, view or any elements included in the view). All script links that are not inline are written to the script block.
One other example I could think of is an element being included in a view or layout multiple times, with the same data. If that element was computationally expensive, you could process once in the view and out to a block, then display that block in the element. The processing only gets done one. Even better, you could have the element output to the block, if it is empty, then output the block, Subsequent calls would just output the block. There are probably alternate solutions using caching as well, but that would be for when element content is the same across multiple calls to the site, rather than just calls to the same element within a single view call.
On Friday, 14 December 2012 18:47:58 UTC+10, resting wrote:
-- On Friday, 14 December 2012 18:47:58 UTC+10, resting wrote:
Thanks for the information.Is there a good example where using a block is more sensible as compared to template?
On Friday, 14 December 2012 07:48:29 UTC+8, Reuben wrote:In CakePHP 2.1, Blocks are a part of the core, and are used to display content, scripts and css as a part of the layout.You can start a block by simply writing to it, using any of the available block commands (i.e. $this->start(), $this->assign(), $this->prepend(), $this->append()), although your mileage may vary if you start appending to a block that hasn't been created.Your example is using trying to use nested elements, rather than blocks.The strength of blocks is that you can write to them from helpers, layouts, views and elements, and then choose to display the block at a later time the view or layout processing.Several helpers could write to a block that is displayed by an element, and element would not need to know about those helpers. Without blocks, the element would need to know about those helpers to display the content.RegardsReuben Helms
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home