Merge pull request #1627 from sime/view-get-default-param

View get default param
This commit is contained in:
Mark Story 2013-09-11 09:05:59 -07:00
commit c5351c5b65
2 changed files with 21 additions and 4 deletions

View file

@ -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 * @return void
*/ */
@ -1674,4 +1674,20 @@ TEXT;
$result = $this->View->fetch('title', $default); $result = $this->View->fetch('title', $default);
$this->assertEquals($expected, $result); $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);
}
} }

View file

@ -582,11 +582,12 @@ class View extends Object {
* Blocks are checked before view variables. * Blocks are checked before view variables.
* *
* @param string $var The view var you want the contents of. * @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])) { if (!isset($this->viewVars[$var])) {
return null; return $default;
} }
return $this->viewVars[$var]; return $this->viewVars[$var];
} }