Skip to content
Browse files

Merge pull request #835 from tigrang/default-block-value

Added ability to set default view block content
  • Loading branch information...
2 parents 07c5102 + d30e092 commit d999a832505cdf26dde7a935ccac750baca34cb9 @lorenzo lorenzo committed Sep 18, 2012
Showing with 23 additions and 7 deletions.
  1. +17 −1 lib/Cake/Test/Case/View/ViewTest.php
  2. +3 −3 lib/Cake/View/View.php
  3. +3 −3 lib/Cake/View/ViewBlock.php
View
18 lib/Cake/Test/Case/View/ViewTest.php
@@ -1445,4 +1445,20 @@ public function testMemoryLeakInPaths() {
$end = memory_get_usage();
$this->assertLessThanOrEqual($start + 5000, $end);
}
-}
+
+/**
+ * tests that a vew block uses default value when not assigned and uses assigned value when it is
+ *
+ * @return void
+ */
+ public function testBlockDefaultValue() {
+ $default = 'Default';
+ $result = $this->View->fetch('title', $default);
+ $this->assertEquals($default, $result);
+
+ $expected = 'My Title';
+ $this->View->assign('title', $expected);
+ $result = $this->View->fetch('title', $default);
+ $this->assertEquals($expected, $result);
+ }
+}
View
6 lib/Cake/View/View.php
@@ -636,11 +636,11 @@ public function assign($name, $value) {
* empty or undefined '' will be returned.
*
* @param string $name Name of the block
- * @return The block content or '' if the block does not exist.
+ * @return string The block content or $default if the block does not exist.
* @see ViewBlock::get()
*/
- public function fetch($name) {
- return $this->Blocks->get($name);
+ public function fetch($name, $default = '') {
+ return $this->Blocks->get($name, $default);
}
/**
View
6 lib/Cake/View/ViewBlock.php
@@ -119,11 +119,11 @@ public function set($name, $value) {
* Get the content for a block.
*
* @param string $name Name of the block
- * @return The block content or '' if the block does not exist.
+ * @return string The block content or $default if the block does not exist.
*/
- public function get($name) {
+ public function get($name, $default = '') {
if (!isset($this->_blocks[$name])) {
- return '';
+ return $default;
}
return $this->_blocks[$name];
}

0 comments on commit d999a83

Please sign in to comment.
Something went wrong with that request. Please try again.