Skip to content

Commit

Permalink
Add View::getBlock()
Browse files Browse the repository at this point in the history
View::get() had two jobs.  That's almost always a bad thing.
Add a new method instead.
  • Loading branch information
markstory committed Dec 18, 2011
1 parent 90f035c commit 7854f9d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
10 changes: 5 additions & 5 deletions lib/Cake/Test/Case/View/ViewTest.php
Expand Up @@ -937,7 +937,7 @@ public function testBlockCapture() {
echo 'Block content';
$this->View->end();

$result = $this->View->get('test');
$result = $this->View->getBlock('test');
$this->assertEquals('Block content', $result);
}

Expand All @@ -955,7 +955,7 @@ public function testBlockCaptureAppend() {
echo ' content';
$this->View->end();

$result = $this->View->get('test');
$result = $this->View->getBlock('test');
$this->assertEquals('Block content', $result);
}

Expand All @@ -966,7 +966,7 @@ public function testBlockCaptureAppend() {
*/
public function testBlockSet() {
$this->View->setBlock('test', 'Block content');
$result = $this->View->get('test');
$result = $this->View->getBlock('test');
$this->assertEquals('Block content', $result);
}

Expand All @@ -979,7 +979,7 @@ public function testBlockAppend() {
$this->View->setBlock('test', 'Block');
$this->View->append('test', ' content');

$result = $this->View->get('test');
$result = $this->View->getBlock('test');
$this->assertEquals('Block content', $result);
}

Expand All @@ -990,7 +990,7 @@ public function testBlockAppend() {
*/
public function testBlockAppendUndefined() {
$this->View->append('test', 'Unknown');
$result = $this->View->get('test');
$result = $this->View->getBlock('test');
$this->assertEquals('Unknown', $result);
}

Expand Down
18 changes: 14 additions & 4 deletions lib/Cake/View/View.php
Expand Up @@ -499,12 +499,9 @@ public function getVar($var) {
* @return mixed The content of the named var if its set, otherwise null.
*/
public function get($var) {
if (!isset($this->viewVars[$var]) && !isset($this->_blocks[$var])) {
if (!isset($this->viewVars[$var])) {
return null;
}
if (isset($this->_blocks[$var])) {
return $this->_blocks[$var];
}
return $this->viewVars[$var];
}

Expand Down Expand Up @@ -577,6 +574,19 @@ public function setBlock($name, $value) {
$this->_blocks[$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.
*/
public function getBlock($name) {
if (!isset($this->_blocks[$name])) {
return '';
}
return $this->_blocks[$name];
}

/**
* End a capturing block. The compliment to View::start()
*
Expand Down

0 comments on commit 7854f9d

Please sign in to comment.