{2480} - 3.9.3.1 Passing Variables into an Element
You can set variables for your elements by passing them as parameters to the element.
<?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 the names of the keys of the passed array (much like how set() in the controller works with view files). In the above example, the /app/views/elements/helpbox.ctp file can use the $helptext variable.
One way to take full advantage of elements is by using requestAction(). The requestAction() function fetches view variables from a controller action and returns them as an array. This allows your elements to perform in true MVC style. Create a controller action that prepares the view variables for your elements, and 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(compact('posts'));
}
}
}
?>
<?phpclass PostsController extends AppController {...function index() {$posts = $this->paginate();if (isset($this->params['requested'])) {return $posts;} else {$this->set(compact('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/order:asc/limit:5'); ?>
<ol>
<li><?php echo $posts['Post']['title']; ?>;</li>
</ol>
<h2>Latest Posts</h2><?php $posts = $this->requestAction('posts/index/sort:created/order:asc/limit:5'); ?><ol><li><?php echo $posts['Post']['title']; ?>;</li></ol>
{5239} - 3.9.3.1 Passing Variables into an Element
You can pass data to the element through the 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 the names of the keys of the passed array (much like how 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));?>
In order to cache different versions of the same element in an application, you need to also send a 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')));?>
One way to take full advantage of elements is by using requestAction(). The requestAction() function fetches view variables from a controller action and returns them as an array. This allows your elements to perform in true MVC style. Create a controller action that prepares the view variables for your elements, and 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(compact('posts'));
}
}
}
?>
<?phpclass PostsController extends AppController {...function index() {$posts = $this->paginate();if (isset($this->params['requested'])) {return $posts;} else {$this->set(compact('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/order: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/order:asc/limit:5'); ?><?php foreach($posts as $post): ?><ol><li><?php echo $post['Post']['title']; ?></li></ol><?php endforeach; ?>
Differences
| Lines: 1-13 | Lines: 1-44 | ||
| <title>Passing Variables into an Element</title> | <title>Passing Variables into an Element</title> | ||
| - | <p>You can set variables for your elements by passing them as parameters to the element.</p> | + | <p>You can pass data to the element through the second argument</p> |
| <pre> | <pre> | ||
| <?php echo | <?php echo | ||
| $this->element('helpbox', | $this->element('helpbox', | ||
| array("helptext" => "Oh, this text is very helpful.")); | array("helptext" => "Oh, this text is very helpful.")); | ||
| ?> | ?> | ||
| </pre> | </pre> | ||
| <p>Inside the Element file, all the passed variables are available as the names of the keys of the passed array (much like how set() in the controller works with view files). In the above example, the <kbd>/app/views/elements/helpbox.ctp</kbd> file can use the <code>$helptext</code> variable. </p> | <p>Inside the Element file, all the passed variables are available as the names of the keys of the passed array (much like how set() in the controller works with view files). In the above example, the <kbd>/app/views/elements/helpbox.ctp</kbd> file can use the <code>$helptext</code> variable. </p> | ||
| + | <pre> | ||
| + | <?php | ||
| + | echo $helptext; //outputs "Oh, this text is very helpful." | ||
| + | ?> | ||
| + | </pre> | ||
| + | <p>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:</p> | ||
| + | <pre> | ||
| + | <?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 | ||
| + | ) | ||
| + | ); | ||
| + | ?> | ||
| + | </pre> | ||
| + | <p>In order to cache different versions of the same element in an application, you need to also send a cache "key" value using the following format:</p> | ||
| + | <pre> | ||
| + | <?php | ||
| + | $this->element('helpbox', | ||
| + | array( | ||
| + | "cache" => array('time'=> "+7 days",'key'=>'unique value') | ||
| + | ) | ||
| + | ); | ||
| + | ?> | ||
| + | </pre> | ||
| <p>One way to take full advantage of elements is by using <code>requestAction()</code>. The <code>requestAction()</code> function fetches view variables from a controller action and returns them as an array. This allows your elements to perform in true MVC style. Create a controller action that prepares the view variables for your elements, and call <code>requestAction()</code> inside the second parameter of <code>element()</code> to feed the element the view variables from your controller.</p> | <p>One way to take full advantage of elements is by using <code>requestAction()</code>. The <code>requestAction()</code> function fetches view variables from a controller action and returns them as an array. This allows your elements to perform in true MVC style. Create a controller action that prepares the view variables for your elements, and call <code>requestAction()</code> inside the second parameter of <code>element()</code> to feed the element the view variables from your controller.</p> | ||
| <p>To do this, in your controller add something like the following for the Post example.</p> | <p>To do this, in your controller add something like the following for the Post example.</p> | ||
| <pre> | <pre> | ||
| <?php | <?php | ||
| Lines: 27-34 | Lines: 58-67 | ||
| <p>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:</p> | <p>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:</p> | ||
| <pre> | <pre> | ||
| <h2>Latest Posts</h2> | <h2>Latest Posts</h2> | ||
| <?php $posts = $this->requestAction('posts/index/sort:created/order:asc/limit:5'); ?> | <?php $posts = $this->requestAction('posts/index/sort:created/order:asc/limit:5'); ?> | ||
| + | <?php foreach($posts as $post): ?> | ||
| <ol> | <ol> | ||
| - | <li><?php echo $posts['Post']['title']; ?>;</li> | + | <li><?php echo $post['Post']['title']; ?></li> |
| </ol> | </ol> | ||
| + | <?php endforeach; ?> | ||
| </pre> | </pre> | ||
