3.5.3.1.1 set
set(string $var, mixed $value)
set(string $var, mixed $value)
set() 方法是从你的控制器向视图传输数据的主要方法.一旦你使用了set(),变量就可以在你的视图中访问了.
<?php
//First you pass data from the controller:
$this->set('color', 'pink');
//Then, in the view, you can utilize the data:
You have selected <?php echo $color; ?> icing for the cake.
?>
<?php//First you pass data from the controller:$this->set('color', 'pink');//Then, in the view, you can utilize the data:You have selected <?php echo $color; ?> icing for the cake.?>
set()方法还可以携带一个数组作为它的第一个参数.这通常是向视图分配一组信息的快速的方法.但要注意的是,在它被分配给视图前,数组键的下划线会被替换掉, (‘underscored_key’ 将会变成‘underscoredKey’):
<?php
$data = array(
'color' => 'pink',
'type' => 'sugar',
'base_price' => 23.95
);
//make $color, $type, and $basePrice
//available to the view:
$this->set($data);
?>
<?php$data = array('color' => 'pink','type' => 'sugar','base_price' => 23.95);//make $color, $type, and $basePrice//available to the view:$this->set($data);?>
See comment for this section
