Browse Source

Throw an exception when starting a view block twice

Marc Würth 12 years ago
parent
commit
39cd7565ef
2 changed files with 18 additions and 0 deletions
  1. 14 0
      lib/Cake/Test/Case/View/ViewTest.php
  2. 4 0
      lib/Cake/View/ViewBlock.php

+ 14 - 0
lib/Cake/Test/Case/View/ViewTest.php

@@ -1516,6 +1516,20 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
+ * Test that starting the same block twice throws an exception
+ *
+ * @expectedException CakeException
+ * @return void
+ */
+	public function testStartBlocksTwice() {
+		$this->View->start('first');
+		echo 'In first ';
+		$this->View->start('second');
+		echo 'In second';
+		$this->View->start('first');
+	}
+
+/**
  * Test that an exception gets thrown when you leave a block open at the end
  * of a view.
  *

+ 4 - 0
lib/Cake/View/ViewBlock.php

@@ -72,9 +72,13 @@ class ViewBlock {
  * using View::get();
  *
  * @param string $name The name of the block to capture for.
+ * @throws CakeException When starting a block twice
  * @return void
  */
 	public function start($name) {
+		if (in_array($name, $this->_active)) {
+			throw new CakeException(__("A view block with the name '%s' is already/still open.", $name));
+		}
 		$this->_active[] = $name;
 		ob_start();
 	}