Browse Source

Merge pull request #3622 from markstory/3.0-view-block

3.0 - Simplify View blocks
Mark Story 11 years ago
parent
commit
bd7e20a368
3 changed files with 34 additions and 72 deletions
  1. 7 16
      src/View/View.php
  2. 8 36
      src/View/ViewBlock.php
  3. 19 20
      tests/TestCase/View/ViewTest.php

+ 7 - 16
src/View/View.php

@@ -611,39 +611,30 @@ class View {
 	}
 
 /**
- * Start capturing output for a 'block' if it has no content
+ * Append to an existing or new block.
  *
- * @param string $name The name of the block to capture for.
- * @return void
- * @see ViewBlock::startIfEmpty()
- */
-	public function startIfEmpty($name) {
-		$this->Blocks->startIfEmpty($name);
-	}
-
-/**
- * Append to an existing or new block. Appending to a new
- * block will create the block.
+ * Appending to a new block will create the block.
  *
  * @param string $name Name of the block
  * @param mixed $value The content for the block.
  * @return void
  * @see ViewBlock::concat()
  */
-	public function append($name, $value = null) {
+	public function append($name, $value) {
 		$this->Blocks->concat($name, $value);
 	}
 
 /**
- * Prepend to an existing or new block. Prepending to a new
- * block will create the block.
+ * Prepend to an existing or new block.
+ *
+ * Prepending to a new block will create the block.
  *
  * @param string $name Name of the block
  * @param mixed $value The content for the block.
  * @return void
  * @see ViewBlock::concat()
  */
-	public function prepend($name, $value = null) {
+	public function prepend($name, $value) {
 		$this->Blocks->concat($name, $value, ViewBlock::PREPEND);
 	}
 

+ 8 - 36
src/View/ViewBlock.php

@@ -1,6 +1,5 @@
 <?php
 /**
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -85,26 +84,6 @@ class ViewBlock {
 	}
 
 /**
- * Start capturing output for a 'block' if it is empty
- *
- * 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::get();
- *
- * @param string $name The name of the block to capture for.
- * @return void
- */
-	public function startIfEmpty($name) {
-		if (empty($this->_blocks[$name])) {
-			return $this->start($name);
-		}
-		$this->_discardActiveBufferOnEnd = true;
-		ob_start();
-	}
-
-/**
  * End a capturing block. The compliment to ViewBlock::start()
  *
  * @return void
@@ -119,10 +98,7 @@ class ViewBlock {
 		if (!empty($this->_active)) {
 			$active = end($this->_active);
 			$content = ob_get_clean();
-			if (!isset($this->_blocks[$active])) {
-				$this->_blocks[$active] = '';
-			}
-			$this->_blocks[$active] .= $content;
+			$this->_blocks[$active] = $content;
 			array_pop($this->_active);
 		}
 	}
@@ -141,18 +117,14 @@ class ViewBlock {
  *   If ViewBlock::PREPEND it will be prepended.
  * @return void
  */
-	public function concat($name, $value = null, $mode = ViewBlock::APPEND) {
-		if (isset($value)) {
-			if (!isset($this->_blocks[$name])) {
-				$this->_blocks[$name] = '';
-			}
-			if ($mode === ViewBlock::PREPEND) {
-				$this->_blocks[$name] = $value . $this->_blocks[$name];
-			} else {
-				$this->_blocks[$name] .= $value;
-			}
+	public function concat($name, $value, $mode = ViewBlock::APPEND) {
+		if (!isset($this->_blocks[$name])) {
+			$this->_blocks[$name] = '';
+		}
+		if ($mode === ViewBlock::PREPEND) {
+			$this->_blocks[$name] = $value . $this->_blocks[$name];
 		} else {
-			$this->start($name);
+			$this->_blocks[$name] .= $value;
 		}
 	}
 

+ 19 - 20
tests/TestCase/View/ViewTest.php

@@ -1128,49 +1128,50 @@ class ViewTest extends TestCase {
  *
  * @return void
  */
-	public function testBlockCapture() {
+	public function testBlockCaptureOverwrite() {
 		$this->View->start('test');
 		echo 'Block content';
 		$this->View->end();
 
+		$this->View->start('test');
+		echo 'New content';
+		$this->View->end();
+
 		$result = $this->View->fetch('test');
-		$this->assertEquals('Block content', $result);
+		$this->assertEquals('New content', $result);
 	}
 
 /**
- * Test block with startIfEmpty
+ * Test that blocks can be fetched inside a block with the same name
  *
  * @return void
  */
-	public function testBlockCaptureStartIfEmpty() {
-		$this->View->startIfEmpty('test');
-		echo "Block content 1";
+	public function testBlockExtend() {
+		$this->View->start('test');
+		echo 'Block content';
 		$this->View->end();
 
-		$this->View->startIfEmpty('test');
-		echo "Block content 2";
+		$this->View->start('test');
+		echo $this->View->fetch('test');
+		echo 'New content';
 		$this->View->end();
 
 		$result = $this->View->fetch('test');
-		$this->assertEquals('Block content 1', $result);
+		$this->assertEquals('Block contentNew content', $result);
 	}
 
 /**
- * Test block with startIfEmpty
+ * Test creating a block with capturing output.
  *
  * @return void
  */
-	public function testBlockCaptureStartStartIfEmpty() {
+	public function testBlockCapture() {
 		$this->View->start('test');
-		echo "Block content 1";
-		$this->View->end();
-
-		$this->View->startIfEmpty('test');
-		echo "Block content 2";
+		echo 'Block content';
 		$this->View->end();
 
 		$result = $this->View->fetch('test');
-		$this->assertEquals('Block content 1', $result);
+		$this->assertEquals('Block content', $result);
 	}
 
 /**
@@ -1183,9 +1184,7 @@ class ViewTest extends TestCase {
 		echo 'Block';
 		$this->View->end();
 
-		$this->View->append('test');
-		echo ' content';
-		$this->View->end();
+		$this->View->append('test', ' content');
 
 		$result = $this->View->fetch('test');
 		$this->assertEquals('Block content', $result);