Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Added: View::startIfEmpty($name) #930

Merged
merged 3 commits into from Dec 1, 2012
Jump to file or symbol
Failed to load files and symbols.
+82 −1
Split
@@ -1217,6 +1217,42 @@ public function testBlockCapture() {
}
/**
+ * Test block with startIfEmpty
+ *
+ * @return void
+ */
+ public function testBlockCaptureStartIfEmpty() {
+ $this->View->startIfEmpty('test');
+ echo "Block content 1";
+ $this->View->end();
+
+ $this->View->startIfEmpty('test');
+ echo "Block content 2";
+ $this->View->end();
+
+ $result = $this->View->fetch('test');
+ $this->assertEquals('Block content 1', $result);
+ }
@ADmad

ADmad Nov 14, 2012

Member

Perhaps you should add 1 more test where you first use start() for a new block and then startIfEmpty().

+
+/**
+ * Test block with startIfEmpty
+ *
+ * @return void
+ */
+ public function testBlockCaptureStartStartIfEmpty() {
+ $this->View->start('test');
+ echo "Block content 1";
+ $this->View->end();
+
+ $this->View->startIfEmpty('test');
+ echo "Block content 2";
+ $this->View->end();
+
+ $result = $this->View->fetch('test');
+ $this->assertEquals('Block content 1', $result);
+ }
+
+/**
* Test appending to a block with capturing output.
*
* @return void
View
@@ -599,7 +599,18 @@ public function start($name) {
}
/**
- * Append to an existing or new block. Appending to a new
+ * Start capturing output for a 'block' if it has no content
+ *
+ * @param string $name The name of the block to capture for.
+ * @return void
+ * @see ViewBlock::startIfEmpty()
+ */
+ public function startIfEmpty($name) {
+ return $this->Blocks->startIfEmpty($name);
+ }
+
+/**
+ * Append to an existing or new block. Appending to a new
* block will create the block.
*
* @param string $name Name of the block
@@ -52,6 +52,15 @@ class ViewBlock {
protected $_active = array();
/**
+ * Should the currently captured content be discarded on ViewBlock::end()
+ *
+ * @var boolean
+ * @see ViewBlock::end()
+ * @see ViewBlock::startIfEmpty()
+ */
+ protected $_discardActiveBufferOnEnd = false;
+
+/**
* Start capturing output for a 'block'
*
* Blocks allow you to create slots or blocks of dynamic content in the layout.
@@ -69,12 +78,37 @@ public function start($name) {
}
/**
+ * 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
* @see ViewBlock::start()
*/
public function end() {
+ if ($this->_discardActiveBufferOnEnd) {
+ $this->_discardActiveBufferOnEnd = false;
+ ob_end_clean();
+ return;
+ }
if (!empty($this->_active)) {
$active = end($this->_active);
$content = ob_get_clean();