3.5.3.1 Interaksi dengan Views
3.5.3.1.1 set
set(string $var, mixed $value)
Method set() adalah cara utama untuk mengirimkan data dari controller ke view. Jika set() sudah dipanggil, maka variabel yang diinginkan bisa diakses dari view.
<?php
//Pertama, anda mengirim data dari controller
$this->set('color', 'pink');
//Kemudian, di dalam view anda bisa menggunakan data tersebut:
?>
You have selected <?php echo $color; ?> icing for the cake.
<?php//Pertama, anda mengirim data dari controller$this->set('color', 'pink');//Kemudian, di dalam view anda bisa menggunakan data tersebut:?>You have selected <?php echo $color; ?> icing for the cake.
Method set() juga bisa menerima array asosiatif sebagai parameter pertamanya. Ini merupakan salah satu cara cepat untuk mengirim beberapa informasi ke view.
Array keys akan di-inflected sebelum dikirim ke view.('underscored_key' akan menjadi 'underscoredKey', dst.):
<?php
$data = array(
'color' => 'pink',
'type' => 'sugar',
'base_price' => 23.95
);
//membuat $color, $type, and $basePrice tersedia di view:
$this->set($data);
?>
<?php$data = array('color' => 'pink','type' => 'sugar','base_price' => 23.95);//membuat $color, $type, and $basePrice tersedia di view:$this->set($data);?>
3.5.3.1.2 render
render(string $action, string $layout, string $file)
The render() method is automatically called at the end of each requested controller action. This method performs all the view logic (using the data you’ve given in using the set() method), places the view inside its layout and serves it back to the end user.
The default view file used by render is determined by convention. If the search() action of the RecipesController is requested, the view file in /app/views/recipes/search.ctp will be rendered.
Although CakePHP will automatically call it (unless you’ve set $this->autoRender to false) after every action’s logic, you can use it to specify an alternate view file by specifying an action name in the controller using $action. You can also specify an alternate view file using the third parameter, $file. When using $file, don't forget to utilize a few of CakePHP’s global constants (such as VIEWS).
The $layout parameter allows you to specify the layout the view is rendered in.
