Add View::getBlock()

View::get() had two jobs.  That's almost always a bad thing.
Add a new method instead.
This commit is contained in:
mark_story 2011-09-14 22:32:16 -04:00
parent 90f035c54d
commit 7854f9dd52
2 changed files with 19 additions and 9 deletions

View file

@ -937,7 +937,7 @@ class ViewTest extends CakeTestCase {
echo 'Block content';
$this->View->end();
$result = $this->View->get('test');
$result = $this->View->getBlock('test');
$this->assertEquals('Block content', $result);
}
@ -955,7 +955,7 @@ class ViewTest extends CakeTestCase {
echo ' content';
$this->View->end();
$result = $this->View->get('test');
$result = $this->View->getBlock('test');
$this->assertEquals('Block content', $result);
}
@ -966,7 +966,7 @@ class ViewTest extends CakeTestCase {
*/
public function testBlockSet() {
$this->View->setBlock('test', 'Block content');
$result = $this->View->get('test');
$result = $this->View->getBlock('test');
$this->assertEquals('Block content', $result);
}
@ -979,7 +979,7 @@ class ViewTest extends CakeTestCase {
$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);
}
@ -990,7 +990,7 @@ class ViewTest extends CakeTestCase {
*/
public function testBlockAppendUndefined() {
$this->View->append('test', 'Unknown');
$result = $this->View->get('test');
$result = $this->View->getBlock('test');
$this->assertEquals('Unknown', $result);
}

View file

@ -499,12 +499,9 @@ class View extends Object {
* @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];
}
@ -577,6 +574,19 @@ class View extends Object {
$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()
*