Permalink
Browse files

Implementing basic features of view 'blocks'

  • Loading branch information...
markstory committed Aug 17, 2011
1 parent 3e2bde5 commit 9107913c075cf711fae0cece1b64c5bfb4f798d2
Showing with 117 additions and 7 deletions.
  1. +14 −5 lib/Cake/Test/Case/View/ViewTest.php
  2. +103 −2 lib/Cake/View/View.php
@@ -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);
}
@@ -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');
@@ -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));
}
}
View
@@ -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.
*
@@ -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;
}
/**

0 comments on commit 9107913

Please sign in to comment.