Splitting View::element() param $params into $data and $options. Removed param $callbacks, use key 'callbacks' in $options array instead. Closes #1646

This commit is contained in:
ADmad 2011-04-17 06:56:33 +05:30
parent d83712ce43
commit c80a51a163
2 changed files with 43 additions and 40 deletions

View file

@ -289,43 +289,44 @@ class View extends Object {
* This realizes the concept of Elements, (or "partial layouts") and the $params array is used to send
* data to be used in the element. Elements can be cached improving performance by using the `cache` option.
*
* ### Special params
*
* @param string $name Name of template file in the/app/views/elements/ folder
* @param array $data Array of data to be made available to the rendered view (i.e. the Element)
* @param array $options Array of options. Possible keys are:
* - `cache` - Can either be `true`, to enable caching using the config in View::$elementCache. Or an array
* If an array, the following keys can be used:
* - `config` - Used to store the cached element in a custom cache configuration.
* - `key` - Used to define the key used in the Cache::write(). It will be prefixed with `element_`
* - `plugin` - Load an element from a specific plugin.
*
* @param string $name Name of template file in the/app/views/elements/ folder
* @param array $params Array of data to be made available to the for rendered
* view (i.e. the Element)
* @param boolean $callbacks Set to true to fire beforeRender and afterRender helper callbacks for this element.
* - `callbacks` - Set to true to fire beforeRender and afterRender helper callbacks for this element.
* Defaults to false.
* @return string Rendered Element
*/
public function element($name, $params = array(), $callbacks = false) {
public function element($name, $data = array(), $options = array()) {
$file = $plugin = $key = null;
$callbacks = false;
if (isset($params['plugin'])) {
$plugin = $params['plugin'];
if (isset($options['plugin'])) {
$plugin = $options['plugin'];
}
if (isset($this->plugin) && !$plugin) {
$plugin = $this->plugin;
}
if (isset($options['callbacks'])) {
$callbacks = $options['callbacks'];
}
if (isset($params['cache'])) {
$keys = array_merge(array($plugin, $name), array_keys($params));
if (isset($options['cache'])) {
$keys = array_merge(array($plugin, $name), array_keys($options), array_keys($data));
$caching = array(
'config' => $this->elementCache,
'key' => implode('_', $keys)
);
if (is_array($params['cache'])) {
if (is_array($options['cache'])) {
$defaults = array(
'config' => $this->elementCache,
'key' => $caching['key']
);
$caching = array_merge($defaults, $params['cache']);
$caching = array_merge($defaults, $options['cache']);
}
$key = 'element_' . $caching['key'];
$contents = Cache::read($key, $caching['config']);
@ -343,11 +344,11 @@ class View extends Object {
if ($callbacks) {
$this->Helpers->trigger('beforeRender', array($file));
}
$element = $this->_render($file, array_merge($this->viewVars, $params));
$element = $this->_render($file, array_merge($this->viewVars, $data));
if ($callbacks) {
$this->Helpers->trigger('afterRender', array($file, $element));
}
if (isset($params['cache'])) {
if (isset($options['cache'])) {
Cache::write($key, $element, $caching['config']);
}
return $element;

View file

@ -427,7 +427,7 @@ class ViewTest extends CakeTestCase {
$result = $this->View->element('test_element');
$this->assertEqual($result, 'this is the test element');
$result = $this->View->element('plugin_element', array('plugin' => 'test_plugin'));
$result = $this->View->element('plugin_element', array(), array('plugin' => 'test_plugin'));
$this->assertEqual($result, 'this is the plugin element using params[plugin]');
$this->View->plugin = 'test_plugin';
@ -451,7 +451,7 @@ class ViewTest extends CakeTestCase {
$this->View->ElementCallbackMockHtml->expects($this->at(0))->method('beforeRender');
$this->View->ElementCallbackMockHtml->expects($this->at(1))->method('afterRender');
$this->View->element('test_element', array(), true);
$this->View->element('test_element', array(), array('callbacks' => true));
$this->mockObjects[] = $this->View->ElementCallbackMockHtml;
}
/**
@ -464,11 +464,11 @@ class ViewTest extends CakeTestCase {
$Controller->helpers = array('Form');
$View = new View($Controller);
$result = $View->element('type_check', array('form' => 'string'), true);
$result = $View->element('type_check', array('form' => 'string'), array('callbacks' => true));
$this->assertEqual('string', $result);
$View->set('form', 'string');
$result = $View->element('type_check', array(), true);
$result = $View->element('type_check', array(), array('callbacks' => true));
$this->assertEqual('string', $result);
}
@ -505,32 +505,34 @@ class ViewTest extends CakeTestCase {
$View = new TestView($this->PostsController);
$View->elementCache = 'test_view';
$result = $View->element('test_element', array('cache' => true));
$result = $View->element('test_element', array(), array('cache' => true));
$expected = 'this is the test element';
$this->assertEquals($expected, $result);
$result = Cache::read('element__test_element_cache', 'test_view');
$this->assertEquals($expected, $result);
$result = $View->element('test_element', array('cache' => true, 'param' => 'one', 'foo' => 'two'));
$result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true));
$this->assertEquals($expected, $result);
$result = Cache::read('element__test_element_cache_param_foo', 'test_view');
$this->assertEquals($expected, $result);
$result = $View->element('test_element', array(
'cache' => array('key' => 'custom_key'),
'param' => 'one',
'foo' => 'two'
), array(
'cache' => array('key' => 'custom_key')
));
$result = Cache::read('element_custom_key', 'test_view');
$this->assertEquals($expected, $result);
$View->elementCache = 'default';
$result = $View->element('test_element', array(
'cache' => array('config' => 'test_view'),
'param' => 'one',
'foo' => 'two'
), array(
'cache' => array('config' => 'test_view'),
));
$result = Cache::read('element__test_element_cache_param_foo', 'test_view');
$this->assertEquals($expected, $result);