P. 1
Cake Php Cookbook 2.0

Cake Php Cookbook 2.0

|Views: 2,566|Likes:
Published by Syed Jibran Uddin
Cake Php 2.0 Documentation
Cake Php 2.0 Documentation

More info:

Published by: Syed Jibran Uddin on Sep 09, 2012
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





New in version 2.1. View extending allows you to wrap one view in another. Combining this with view
gives you a powerful way to keep your views DRY. For example, your application has a sidebar that
needs to change depending on the specific view being rendered. By extending a common view file you can
avoid repeating the common markup for your sidebar, and only define the parts that change:

// app/View/Common/view.ctp

echo $this->fetch(’title’); ?>

echo $this->fetch(’content’); ?>

Related actions

    echo $this->fetch(’sidebar’); ?>

The above view file could be used as a parent view. It expects that the view extending it will define the
sidebarandtitleblocks. Thecontentblock is a special block that CakePHP creates. It will contain
all the un-captured content from the extending view. Assuming our view file has a $posts variable with
the data about our post. Our view could look like:

// app/View/Posts/view.ctp


$this->assign(’title’, $post);



  • echo $this->Html->link(’edit’, array(
    ’action’ => ’edit’,

    )); ?>

  • $this->end(); ?>

    // The remaining content will be available as the ’content’ block
    // in the parent view.

    echo h($post[’Post’][’body’]);

    The post view above shows how you can extend a view, and populate a set of blocks. Any content not


    Chapter 7. Views

    CakePHP Cookbook Documentation, Release 2.x

    in already in a defined block will captured and put into a special block named content. When a view
    contains a call to extend() execution continues to the bottom of the current view file. Once its complete,
    the extended view will be rendered. Calling extend() more than once in a view file will override the
    parent view that will be processed next:


    The above will result in /Common/index.ctp being rendered as the parent view to the current view.

    You can nest extended views as many times as necessary. Each view can extend another view if desired.
    Each parent view will get the previous view’s content as the content block.

    Note: You should avoid using content as a block name in your application. CakePHP uses this for
    un-captured content in extended views.

    You're Reading a Free Preview

    /*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->