Difference between #2 and #4 of
pass variables or content block from view file to layout file

Changes

Title unchanged

pass variables or content block from view file to layout file

Category unchanged

How-tos

Yii version unchanged

Tags unchanged

CClipWidget, layouts

Content changed

iIf you use yiic to generate webApplication there will bexists a layouts di folder under the protected/views/, and the Controller class under protected/components which will use by default use these layouts files . aAll your controllers may extends the Controller base class directly or indirectly . pPlease look at thisat class, you may find this lines : ```php /**      * @var string the default layout for the controller view. Defaults to '//layouts/column1',      * meaning using a single column layout. See 'protected/views/layouts/column1.php'.     */
 
    
*/
 
public $layout = '//layouts/column1';     /**      * @var array context menu items. This property will be assigned to {@link CMenu::items}.      */     public $menu = array();     /**      * @var array the breadcrumbs of the current page. The value of this property will      * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}      * for more details on how to specify this property.     */
 
    
*/
 
public $breadcrumbs = array(); ``` these variables will be shared by all its subClasses (may be your whole XXXController of your project ) ). except the $layout variable the other two usually used to pass variables to layout files : ```php $this->breadcrumbs=array(     'Albums'=>array('index'),     'Manage', ); $this->menu=array(     array('label'=>'List Album','url'=>array('index')),     array('label'=>'Create Album','url'=>array('create')), ); ``` iIf you use gii to generate crud functionality you may see such code in the top section of file (admin.php, index.php, create.php, update.php, view....). "$this" represent the current controller , so "breadcrumbs" and "menu" are the member variables of controller and
 
it defined in "Controller" class .   n
 
 
N
ow lets take look at layout file "column2":
 

```php
<?php $this->beginContent('//layouts/main'); ?>
[...]
'links'=>$this->breadcrumbs,
)); ?><!-- breadcrumbs -->
<?php endif?>
..
[...]
in yii when some route be executed , lets say (default manner) : "user/create" , UserController::actionCreate function will be called , and in the actionCreate function it
will render the create.php view file and using some specified layout file . so the execution order will be : create.php(
<<view file>>) ----> column2.php(<<layout file>>)----> main.php(<<layout file>>) . all these file can refer to "$this" variable , so you can pass variable by defining some public var in the "Controller" class . and give it some value in view file then fetch it in layouts file .
all above method may be the normal way :) ;
[...]
```
**
 
usage  **  
 
in you base controller :
[...]
that's all ! hope help some body ;

here are some topic about it:

 
 
[Yii - On what circumstances should we use clips? ](http://www.yiiframework.com/forum/index.php/topic/31889-yii-on-what-circumstances-should-we-use-clips/ "Yii - On what circumstances should we use clips? ")

[Render Cgridview Pager Separately](http://www.yiiframework.com/forum/index.php/topic/36547-render-cgridview-pager-separately/ "Render Cgridview Pager Separately")
[...]
2 0
6 followers
Viewed: 50 465 times
Version: 1.1
Category: How-tos
Written by: yiqing95
Last updated by: Maurizio Domba Cerin
Created on: Oct 26, 2012
Last updated: 11 years ago
Update Article

Revisions

View all history