Cakephp Classroom image

osdyui
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|Last
Lessons:-Views

Passing Variables into an Element

You can pass data to an element through the element’s second argument:

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 Controller::set() in the controller works with view files). In the above example, the
/app/View/Elements/helpbox.ctp file can use the $helptext variable:

// Inside app/View/Elements/helpbox.ctp
echo $helptext; // Outputs "Oh, this text is very helpful."

The View::element() method also supports options for the element. The options supported are ‘cache’
and ‘callbacks’. An example:

echo $this->element('helpbox', array(
"helptext" => "This is passed to the element as $helptext",
"foobar" => "This is passed to the element as $foobar",
),
array(
// Uses the "long_view" cache configuration
"cache" => "long_view",
// Set to true to have before/afterRender called for the element
"callbacks" => true
)
);

Element caching is facilitated through the Cache class. You can configure elements to be stored in any
Cache configuration you’ve set up. This gives you a great amount of flexibility to decide where and for
how long elements are stored. To cache different versions of the same element in an application, provide a
unique cache key value using the following format:

$this->element('helpbox', array(), array(
"cache" => array('config' => 'short', 'key' => 'unique value')
)
);

You can take full advantage of elements by using requestAction(), which 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:

class PostsController extends AppController {
// ...
public function index() {
$posts = $this->paginate();
if ($this->request->is('requested')) {
return $posts;
}
$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'
);
?>
<ol>
<?php foreach ($posts as $post): ?>
<li><?php echo $post['Post']['title']; ?></li>
<?php endforeach; ?>
</ol>

Caching Elements

You can take advantage of CakePHP view caching if you supply a cache parameter. If set to true, it will
cache the element in the ‘default’ Cache configuration. Otherwise, you can set which cache configuration
should be used. See Caching for more information on configuring Cache. A simple example of caching an
element would be:

echo $this->element('helpbox', array(), array('cache' => true));

If you render the same element more than once in a view and have caching enabled, be sure to set the
‘key’ parameter to a different name each time. This will prevent each successive call from overwriting the
previous element() call’s cached result. For example:

echo $this->element(
'helpbox',
array('var' => $var),
array('cache' => array('key' => 'first_use', 'config' => 'view_long')
);
echo $this->element(
'helpbox',
array('var' => $differenVar),
array('cache' => array('key' => 'second_use', 'config' => 'view_long')
);

The above will ensure that both element results are cached separately. If you want all element caching to
use the same cache configuration, you can avoid some repetition by setting View::$elementCache to
the cache configuration you want to use. CakePHP will use this configuration when none is given.

Requesting Elements from a Plugin

To load an element from a plugin, use the plugin option (moved out of the data option in 1.x):

echo $this->element('helpbox', array(), array('plugin' => 'Contacts'));

If you are using a plugin and wish to use elements from within the plugin, just use the familiar plugin syntax.
If the view is being rendered for a plugin controller/action, the plugin name will automatically be prefixed
onto all elements used, unless another plugin name is present. If the element doesn’t exist in the plugin, it
will look in the main APP folder.

echo $this->element('Contacts.helpbox');

If your view is a part of a plugin, you can omit the plugin name. For example, if you are in the
ContactsController of the Contacts plugin, the following:

echo $this->element('helpbox');
// and
echo $this->element('Contacts.helpbox');

are equivalent and will result in the same element being rendered.

Changed in version 2.1: The $options[plugin] option was deprecated and support for
Plugin.element was added.

 
 
 

osdyui

Skills    Cakephp

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)