Skip to content

Commit

Permalink
Implementing basic features of view 'blocks'
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 18, 2011
1 parent 3e2bde5 commit 9107913
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 7 deletions.
19 changes: 14 additions & 5 deletions lib/Cake/Test/Case/View/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ public function testBlockCaptureAppend() {
* @return void
*/
public function testBlockSet() {
$this->View->set('test', 'Block content');
$this->View->setBlock('test', 'Block content');
$result = $this->View->get('test');
$this->assertEquals('Block content', $result);
}
Expand All @@ -976,7 +976,7 @@ public function testBlockSet() {
* @return void
*/
public function testBlockAppend() {
$this->View->set('test', 'Block');
$this->View->setBlock('test', 'Block');
$this->View->append('test', ' content');

$result = $this->View->get('test');
Expand All @@ -995,13 +995,22 @@ public function testBlockAppendUndefined() {
}

/**
* Appending to an array should cause an exception.
* setting an array should cause an exception.
*
* @expectedException CakeException
* @return void
*/
public function testBlockSetArrayException() {
$this->View->setBlock('test', array(1, 2, 3));
}

/**
* Appending an array should cause an exception.
*
* @expectedException CakeException
* @return void
*/
public function testBlockAppendArrayException() {
$this->View->set('test', array(1, 2, 3));
$this->View->append('test', 'Kaboom');
$this->View->append('test', array(1, 2, 3));
}
}
105 changes: 103 additions & 2 deletions lib/Cake/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,20 @@ class View extends Object {
*/
protected $_scripts = array();

/**
* Block content. An array of blocks indexed by name.
*
* @var array
*/
protected $_blocks = array();

/**
* Name of the block being captured.
*
* @var string
*/
private $__activeBlock = null;

/**
* Holds an array of paths.
*
Expand Down Expand Up @@ -465,13 +479,100 @@ public function getVars() {
*
* @param string $var The view var you want the contents of.
* @return mixed The content of the named var if its set, otherwise null.
* @deprecated Will be removed in 3.0 Use View::get() instead.
*/
public function getVar($var) {
if (!isset($this->viewVars[$var])) {
return $this->get($var);
}

/**
* Returns the contents of the given View variable or a block.
* Blocks are checked before view variables.
*
* @param string $var The view var you want the contents of.
* @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])) {
return null;
}
if (isset($this->_blocks[$var])) {
return $this->_blocks[$var];
}
return $this->viewVars[$var];
}

/**
* Start capturing output for a 'block'
*
* Blocks allow you to create slots or blocks of dynamic content in the layout.
* view files can implement some or all of a layout's slots.
*
* You can end capturing blocks using View::end(). Blocks can be output
* using View::output();
*
* @param string $name The name of the block to capture for.
* @return void
*/
public function start($name) {
$this->_activeBlock = $name;
ob_start();
}

/**
* Append to an existing or new block. Appending to a new
* block will create the block.
*
* Calling append() without a value will create a new capturing
* block that needs to be finished with View::end()
*
* @param string $name Name of the block
* @param string $value The content for the block.
* @return void
* @throws CakeException when you use non-string values.
*/
public function append($name, $value = null) {
if (isset($value)) {
if (!is_string($value)) {
throw new CakeException(__d('cake_dev', '$value must be a string.'));
}
if (!isset($this->_blocks[$name])) {
$this->_blocks[$name] = '';
}
$this->_blocks[$name] .= $value;
} else {
return $this->viewVars[$var];
$this->start($name);
}
}

/**
* Set the content for a block. This will overwrite any
* existing content.
*
* @param string $name Name of the block
* @param string $value The content for the block.
* @return void
* @throws CakeException when you use non-string values.
*/
public function setBlock($name, $value) {
if (!is_string($value)) {
throw new CakeException(__d('cake_dev', 'You can only append strings.'));
}
$this->_blocks[$name] = $value;
}

/**
* End a capturing block. The compliment to View::start()
*
* @return void
* @see View::start()
*/
public function end() {
if (!empty($this->_activeBlock)) {
$content = ob_end_clean();
$this->_blocks[$this->_activeBlock] = $content;
}
$this->_activeBlock = null;
}

/**
Expand Down

0 comments on commit 9107913

Please sign in to comment.