mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Merge pull request #1627 from sime/view-get-default-param
View get default param
This commit is contained in:
commit
c5351c5b65
2 changed files with 21 additions and 4 deletions
|
@ -1660,7 +1660,7 @@ TEXT;
|
|||
}
|
||||
|
||||
/**
|
||||
* Tests that a vew block uses default value when not assigned and uses assigned value when it is
|
||||
* Tests that a view block uses default value when not assigned and uses assigned value when it is
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
|
@ -1674,4 +1674,20 @@ TEXT;
|
|||
$result = $this->View->fetch('title', $default);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that a view variable uses default value when not assigned and uses assigned value when it is
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testViewVarDefaultValue() {
|
||||
$default = 'Default';
|
||||
$result = $this->View->get('title', $default);
|
||||
$this->assertEquals($default, $result);
|
||||
|
||||
$expected = 'Back to the Future';
|
||||
$this->View->set('title', $expected);
|
||||
$result = $this->View->get('title', $default);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -582,11 +582,12 @@ class View extends Object {
|
|||
* Blocks are checked before view variables.
|
||||
*
|
||||
* @param string $var The view var you want the contents of.
|
||||
* @return mixed The content of the named var if its set, otherwise null.
|
||||
* @param mixed $default The default/fallback content of $var.
|
||||
* @return mixed The content of the named var if its set, otherwise $default.
|
||||
*/
|
||||
public function get($var) {
|
||||
public function get($var, $default = null) {
|
||||
if (!isset($this->viewVars[$var])) {
|
||||
return null;
|
||||
return $default;
|
||||
}
|
||||
return $this->viewVars[$var];
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue