Browse Source

Helper method to check if a view block exists or not

Walther Lalk 11 years ago
parent
commit
038980a1fa
3 changed files with 32 additions and 0 deletions
  1. 11 0
      src/View/View.php
  2. 10 0
      src/View/ViewBlock.php
  3. 11 0
      tests/TestCase/View/ViewTest.php

+ 11 - 0
src/View/View.php

@@ -589,6 +589,17 @@ class View {
 	}
 
 /**
+ * Check if a block exists
+ *
+ * @param string $name Name of the block
+ *
+ * @return bool
+ */
+	public function exists($name) {
+		return $this->Blocks->exists($name);
+	}
+
+/**
  * Provides view or element extension/inheritance. Views can extends a
  * parent view and populate blocks in the parent template.
  *

+ 10 - 0
src/View/ViewBlock.php

@@ -155,6 +155,16 @@ class ViewBlock {
 	}
 
 /**
+ * Check if a block exists
+ *
+ * @param string $name Name of the block
+ * @return bool
+ */
+	public function exists($name) {
+		return isset($this->_blocks[$name]);
+	}
+
+/**
  * Get the names of all the existing blocks.
  *
  * @return array An array containing the blocks.

+ 11 - 0
tests/TestCase/View/ViewTest.php

@@ -1349,6 +1349,17 @@ class ViewTest extends TestCase {
 	}
 
 /**
+ * Test checking a block's existance.
+ *
+ * @return void
+ */
+	public function testBlockExist() {
+		$this->assertFalse($this->View->exists('test'));
+		$this->View->assign('test', 'Block content');
+		$this->assertTrue($this->View->exists('test'));
+	}
+
+	/**
  * Test setting a block's content to null
  *
  * @return void