Skip to content

Commit

Permalink
Helper method to check if a view block exists or not
Browse files Browse the repository at this point in the history
  • Loading branch information
Walther Lalk committed Dec 11, 2014
1 parent e6762a6 commit 038980a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,17 @@ public function end() {
$this->Blocks->end();
}

/**
* 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.
Expand Down
10 changes: 10 additions & 0 deletions src/View/ViewBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ public function get($name, $default = '') {
return $this->_blocks[$name];
}

/**
* 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.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase/View/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,17 @@ public function testBlockReset() {
}

/**
* 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
Expand Down

0 comments on commit 038980a

Please sign in to comment.