FAQ
Why do we need a block? And where do we define it?

I think templates are flexible, and I use them as partials and "blocks".
Therefore I'm curious about how to use a block.

For example, I could do this (if I'm not wrong):

Views/index.ctp

<?php echo $this->element('someBlock'); ?>

Then in Views/Elements/someBlock.ctp

<div>
//some layout
<?php echo $this->element('someBlockContent'); ?>
</div>

Then finally in Views/Elements/someBlockContent.ctp

<div>
<p>This is someBlockContent's content</p>
</div>

Nested templates works the same as a block?

--
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.

Search Discussions

  • Resting at Dec 13, 2012 at 9:00 am
    Was referring to this view block
    http://book.cakephp.org/2.0/en/views.html#using-view-blocks

    --
    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.
  • Reuben at Dec 13, 2012 at 11:48 pm
    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.

    Regards
    Reuben Helms
    On Thursday, 13 December 2012 18:46:48 UTC+10, resting wrote:

    Why do we need a block? And where do we define it?

    I think templates are flexible, and I use them as partials and "blocks".
    Therefore I'm curious about how to use a block.

    For example, I could do this (if I'm not wrong):

    Views/index.ctp

    <?php echo $this->element('someBlock'); ?>

    Then in Views/Elements/someBlock.ctp

    <div>
    //some layout
    <?php echo $this->element('someBlockContent'); ?>
    </div>

    Then finally in Views/Elements/someBlockContent.ctp

    <div>
    <p>This is someBlockContent's content</p>
    </div>

    Nested templates works the same as a block?
    --
    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.
  • Resting at Dec 14, 2012 at 8:48 am
    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.

    Regards
    Reuben 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.
  • Reuben at Dec 16, 2012 at 10:43 pm
    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:

    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.

    Regards
    Reuben 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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcake-php @
categoriesphp
postedDec 13, '12 at 8:46a
activeDec 16, '12 at 10:43p
posts5
users2
websitecakephp.org
irc#cakephp

2 users in discussion

Resting: 3 posts Reuben: 2 posts

People

Translate

site design / logo © 2022 Grokbase