From 0c3197a435ee319ac78a6f7dff8255f0abc7892c Mon Sep 17 00:00:00 2001 From: Simon Males Date: Wed, 11 Sep 2013 11:10:27 +0800 Subject: [PATCH 1/3] View::get() to support a fallback param --- lib/Cake/View/View.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/Cake/View/View.php b/lib/Cake/View/View.php index 79fd648fa..af3c6574b 100644 --- a/lib/Cake/View/View.php +++ b/lib/Cake/View/View.php @@ -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. + * @param mixed $default The default/fallback content of $var. * @return mixed The content of the named var if its set, otherwise null. */ - public function get($var) { + public function get($var, $default = null) { if (!isset($this->viewVars[$var])) { - return null; + return $default; } return $this->viewVars[$var]; } From 50ad043092070a1598a6d1a80a40b4f6565e3ed7 Mon Sep 17 00:00:00 2001 From: Simon Males Date: Wed, 11 Sep 2013 22:22:50 +0800 Subject: [PATCH 2/3] View::get() default param test --- lib/Cake/Test/Case/View/ViewTest.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/View/ViewTest.php b/lib/Cake/Test/Case/View/ViewTest.php index 78a149bb0..fdf52d6e2 100644 --- a/lib/Cake/Test/Case/View/ViewTest.php +++ b/lib/Cake/Test/Case/View/ViewTest.php @@ -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); + } } From 8311a16ebd6c1cf71bf7e8b3a8b05ad3c30e48ea Mon Sep 17 00:00:00 2001 From: Simon Males Date: Wed, 11 Sep 2013 22:28:09 +0800 Subject: [PATCH 3/3] Accurate description of what is returned --- lib/Cake/View/View.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/View/View.php b/lib/Cake/View/View.php index af3c6574b..6292f926f 100644 --- a/lib/Cake/View/View.php +++ b/lib/Cake/View/View.php @@ -583,7 +583,7 @@ class View extends Object { * * @param string $var The view var you want the contents of. * @param mixed $default The default/fallback content of $var. - * @return mixed The content of the named var if its set, otherwise null. + * @return mixed The content of the named var if its set, otherwise $default. */ public function get($var, $default = null) { if (!isset($this->viewVars[$var])) {