mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
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:
parent
d83712ce43
commit
c80a51a163
2 changed files with 43 additions and 40 deletions
|
@ -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
|
* 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.
|
* 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
|
* - `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:
|
* If an array, the following keys can be used:
|
||||||
* - `config` - Used to store the cached element in a custom cache configuration.
|
* - `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_`
|
* - `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.
|
* - `plugin` - Load an element from a specific plugin.
|
||||||
*
|
* - `callbacks` - Set to true to fire beforeRender and afterRender helper callbacks for this element.
|
||||||
* @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.
|
|
||||||
* Defaults to false.
|
* Defaults to false.
|
||||||
* @return string Rendered Element
|
* @return string Rendered Element
|
||||||
*/
|
*/
|
||||||
public function element($name, $params = array(), $callbacks = false) {
|
public function element($name, $data = array(), $options = array()) {
|
||||||
$file = $plugin = $key = null;
|
$file = $plugin = $key = null;
|
||||||
|
$callbacks = false;
|
||||||
|
|
||||||
if (isset($params['plugin'])) {
|
if (isset($options['plugin'])) {
|
||||||
$plugin = $params['plugin'];
|
$plugin = $options['plugin'];
|
||||||
}
|
}
|
||||||
if (isset($this->plugin) && !$plugin) {
|
if (isset($this->plugin) && !$plugin) {
|
||||||
$plugin = $this->plugin;
|
$plugin = $this->plugin;
|
||||||
}
|
}
|
||||||
|
if (isset($options['callbacks'])) {
|
||||||
|
$callbacks = $options['callbacks'];
|
||||||
|
}
|
||||||
|
|
||||||
if (isset($params['cache'])) {
|
if (isset($options['cache'])) {
|
||||||
$keys = array_merge(array($plugin, $name), array_keys($params));
|
$keys = array_merge(array($plugin, $name), array_keys($options), array_keys($data));
|
||||||
$caching = array(
|
$caching = array(
|
||||||
'config' => $this->elementCache,
|
'config' => $this->elementCache,
|
||||||
'key' => implode('_', $keys)
|
'key' => implode('_', $keys)
|
||||||
);
|
);
|
||||||
if (is_array($params['cache'])) {
|
if (is_array($options['cache'])) {
|
||||||
$defaults = array(
|
$defaults = array(
|
||||||
'config' => $this->elementCache,
|
'config' => $this->elementCache,
|
||||||
'key' => $caching['key']
|
'key' => $caching['key']
|
||||||
);
|
);
|
||||||
$caching = array_merge($defaults, $params['cache']);
|
$caching = array_merge($defaults, $options['cache']);
|
||||||
}
|
}
|
||||||
$key = 'element_' . $caching['key'];
|
$key = 'element_' . $caching['key'];
|
||||||
$contents = Cache::read($key, $caching['config']);
|
$contents = Cache::read($key, $caching['config']);
|
||||||
|
@ -343,11 +344,11 @@ class View extends Object {
|
||||||
if ($callbacks) {
|
if ($callbacks) {
|
||||||
$this->Helpers->trigger('beforeRender', array($file));
|
$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) {
|
if ($callbacks) {
|
||||||
$this->Helpers->trigger('afterRender', array($file, $element));
|
$this->Helpers->trigger('afterRender', array($file, $element));
|
||||||
}
|
}
|
||||||
if (isset($params['cache'])) {
|
if (isset($options['cache'])) {
|
||||||
Cache::write($key, $element, $caching['config']);
|
Cache::write($key, $element, $caching['config']);
|
||||||
}
|
}
|
||||||
return $element;
|
return $element;
|
||||||
|
|
|
@ -427,7 +427,7 @@ class ViewTest extends CakeTestCase {
|
||||||
$result = $this->View->element('test_element');
|
$result = $this->View->element('test_element');
|
||||||
$this->assertEqual($result, 'this is the 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->assertEqual($result, 'this is the plugin element using params[plugin]');
|
||||||
|
|
||||||
$this->View->plugin = 'test_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(0))->method('beforeRender');
|
||||||
$this->View->ElementCallbackMockHtml->expects($this->at(1))->method('afterRender');
|
$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;
|
$this->mockObjects[] = $this->View->ElementCallbackMockHtml;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -464,11 +464,11 @@ class ViewTest extends CakeTestCase {
|
||||||
$Controller->helpers = array('Form');
|
$Controller->helpers = array('Form');
|
||||||
|
|
||||||
$View = new View($Controller);
|
$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);
|
$this->assertEqual('string', $result);
|
||||||
|
|
||||||
$View->set('form', 'string');
|
$View->set('form', 'string');
|
||||||
$result = $View->element('type_check', array(), true);
|
$result = $View->element('type_check', array(), array('callbacks' => true));
|
||||||
$this->assertEqual('string', $result);
|
$this->assertEqual('string', $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -505,32 +505,34 @@ class ViewTest extends CakeTestCase {
|
||||||
$View = new TestView($this->PostsController);
|
$View = new TestView($this->PostsController);
|
||||||
$View->elementCache = 'test_view';
|
$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';
|
$expected = 'this is the test element';
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
$result = Cache::read('element__test_element_cache', 'test_view');
|
$result = Cache::read('element__test_element_cache', 'test_view');
|
||||||
$this->assertEquals($expected, $result);
|
$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);
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
$result = Cache::read('element__test_element_cache_param_foo', 'test_view');
|
$result = Cache::read('element__test_element_cache_param_foo', 'test_view');
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
$result = $View->element('test_element', array(
|
$result = $View->element('test_element', array(
|
||||||
'cache' => array('key' => 'custom_key'),
|
|
||||||
'param' => 'one',
|
'param' => 'one',
|
||||||
'foo' => 'two'
|
'foo' => 'two'
|
||||||
|
), array(
|
||||||
|
'cache' => array('key' => 'custom_key')
|
||||||
));
|
));
|
||||||
$result = Cache::read('element_custom_key', 'test_view');
|
$result = Cache::read('element_custom_key', 'test_view');
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
$View->elementCache = 'default';
|
$View->elementCache = 'default';
|
||||||
$result = $View->element('test_element', array(
|
$result = $View->element('test_element', array(
|
||||||
'cache' => array('config' => 'test_view'),
|
|
||||||
'param' => 'one',
|
'param' => 'one',
|
||||||
'foo' => 'two'
|
'foo' => 'two'
|
||||||
|
), array(
|
||||||
|
'cache' => array('config' => 'test_view'),
|
||||||
));
|
));
|
||||||
$result = Cache::read('element__test_element_cache_param_foo', 'test_view');
|
$result = Cache::read('element__test_element_cache_param_foo', 'test_view');
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
|
|
Loading…
Add table
Reference in a new issue