3.10.3.1 Passing Variables into an Element
You can pass data to an element through the element's second argument:
<?php echo
$this->element('helpbox',
array("helptext" => "Oh, this text is very helpful."));
?>
<?php echo$this->element('helpbox',array("helptext" => "Oh, this text is very helpful."));?>
Inside the element file, all the passed variables are available as members of the parameter array (in the same way that set() in the controller works with view files). In the above example, the /app/views/elements/helpbox.ctp file can use the $helptext variable.
<?php echo $helptext; //outputs "Oh, this text is very helpful." ?>
<?phpecho $helptext; //outputs "Oh, this text is very helpful."?>
The element() function combines options for the element with the data for the element to pass. The two options are 'cache' and 'plugin'. An example:
<?php echo
$this->element('helpbox',
array(
"helptext" => "This is passed to the element as $helptext",
"foobar" => "This is passed to the element as $foobar",
"cache" => "+2 days", //sets the caching to +2 days.
"plugin" => "" //to render an element from a plugin
)
);
?>
<?php echo$this->element('helpbox',array("helptext" => "This is passed to the element as $helptext","foobar" => "This is passed to the element as $foobar","cache" => "+2 days", //sets the caching to +2 days."plugin" => "" //to render an element from a plugin));?>
To cache different versions of the same element in an application, provide a unique cache key value using the following format:
<?php
$this->element('helpbox',
array(
"cache" => array('time'=> "+7 days",'key'=>'unique value')
)
);
?>
<?php$this->element('helpbox',array("cache" => array('time'=> "+7 days",'key'=>'unique value')));?>
You can take full advantage of elements by using requestAction(). The requestAction() function fetches view variables from a controller action and returns them as an array. This enables your elements to perform in true MVC style. Create a controller action that prepares the view variables for your elements, then call requestAction() inside the second parameter of element() to feed the element the view variables from your controller.
To do this, in your controller add something like the following for the Post example.
<?php
class PostsController extends AppController {
...
function index() {
$posts = $this->paginate();
if (isset($this->params['requested'])) {
return $posts;
} else {
$this->set('posts', $posts);
}
}
}
?>
<?phpclass PostsController extends AppController {...function index() {$posts = $this->paginate();if (isset($this->params['requested'])) {return $posts;} else {$this->set('posts', $posts);}}}?>
And then in the element we can access the paginated posts model. To get the latest five posts in an ordered list we would do something like the following:
<h2>Latest Posts</h2>
<?php $posts = $this->requestAction('posts/index/sort:created/direction:asc/limit:5'); ?>
<?php foreach($posts as $post): ?>
<ol>
<li><?php echo $post['Post']['title']; ?></li>
</ol>
<?php endforeach; ?>
<h2>Latest Posts</h2><?php $posts = $this->requestAction('posts/index/sort:created/direction:asc/limit:5'); ?><?php foreach($posts as $post): ?><ol><li><?php echo $post['Post']['title']; ?></li></ol><?php endforeach; ?>


























