2011-09-30 02:11:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-09-30 02:11:15 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:46 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-09-30 02:11:15 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @since CakePHP(tm) v2.1
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* ViewBlock implements the concept of Blocks or Slots in the View layer.
|
|
|
|
* Slots or blocks are combined with extending views and layouts to afford slots
|
|
|
|
* of content that are present in a layout or parent view, but are defined by the child
|
|
|
|
* view or elements used in the view.
|
|
|
|
*
|
|
|
|
* @package Cake.View
|
|
|
|
*/
|
|
|
|
class ViewBlock {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block content. An array of blocks indexed by name.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_blocks = array();
|
|
|
|
|
|
|
|
/**
|
2011-12-13 03:08:03 +00:00
|
|
|
* The active blocks being captured.
|
2011-09-30 02:11:15 +00:00
|
|
|
*
|
2011-12-13 03:08:03 +00:00
|
|
|
* @var array
|
2011-09-30 02:11:15 +00:00
|
|
|
*/
|
2011-12-13 03:08:03 +00:00
|
|
|
protected $_active = array();
|
2011-09-30 02:11:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start capturing output for a 'block'
|
|
|
|
*
|
|
|
|
* 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 start($name) {
|
2011-12-13 03:08:03 +00:00
|
|
|
$this->_active[] = $name;
|
2011-09-30 02:11:15 +00:00
|
|
|
ob_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* End a capturing block. The compliment to ViewBlock::start()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @see ViewBlock::start()
|
|
|
|
*/
|
|
|
|
public function end() {
|
|
|
|
if (!empty($this->_active)) {
|
2011-12-13 03:08:03 +00:00
|
|
|
$active = end($this->_active);
|
2011-09-30 02:11:15 +00:00
|
|
|
$content = ob_get_clean();
|
2011-12-13 03:08:03 +00:00
|
|
|
if (!isset($this->_blocks[$active])) {
|
|
|
|
$this->_blocks[$active] = '';
|
2011-09-30 02:11:15 +00:00
|
|
|
}
|
2011-12-13 03:08:03 +00:00
|
|
|
$this->_blocks[$active] .= $content;
|
|
|
|
array_pop($this->_active);
|
2011-09-30 02:11:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append to an existing or new block. Appending to a new
|
|
|
|
* block will create the block.
|
|
|
|
*
|
|
|
|
* Calling append() without a value will create a new capturing
|
|
|
|
* block that needs to be finished with View::end(). The content
|
|
|
|
* of the new capturing context will be added to the existing block context.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the block
|
|
|
|
* @param string $value The content for the block.
|
|
|
|
* @return void
|
|
|
|
* @throws CakeException when you use non-string values.
|
|
|
|
*/
|
|
|
|
public function append($name, $value = null) {
|
|
|
|
if (isset($value)) {
|
|
|
|
if (!is_string($value)) {
|
|
|
|
throw new CakeException(__d('cake_dev', '$value must be a string.'));
|
|
|
|
}
|
|
|
|
if (!isset($this->_blocks[$name])) {
|
|
|
|
$this->_blocks[$name] = '';
|
|
|
|
}
|
|
|
|
$this->_blocks[$name] .= $value;
|
|
|
|
} else {
|
|
|
|
$this->start($name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the content for a block. This will overwrite any
|
|
|
|
* existing content.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the block
|
|
|
|
* @param string $value The content for the block.
|
|
|
|
* @return void
|
|
|
|
* @throws CakeException when you use non-string values.
|
|
|
|
*/
|
|
|
|
public function set($name, $value) {
|
|
|
|
if (!is_string($value)) {
|
|
|
|
throw new CakeException(__d('cake_dev', 'Blocks can only contain strings.'));
|
|
|
|
}
|
|
|
|
$this->_blocks[$name] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the content for a block.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the block
|
|
|
|
* @return The block content or '' if the block does not exist.
|
|
|
|
*/
|
|
|
|
public function get($name) {
|
|
|
|
if (!isset($this->_blocks[$name])) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return $this->_blocks[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the names of all the existing blocks.
|
|
|
|
*
|
|
|
|
* @return array An array containing the blocks.
|
|
|
|
*/
|
|
|
|
public function keys() {
|
|
|
|
return array_keys($this->_blocks);
|
|
|
|
}
|
2011-12-13 02:43:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the currently open block.
|
|
|
|
*
|
2011-12-13 03:08:03 +00:00
|
|
|
* @return mixed Either null or the name of the last open block.
|
2011-12-13 02:43:41 +00:00
|
|
|
*/
|
|
|
|
public function active() {
|
2011-12-13 03:08:03 +00:00
|
|
|
return end($this->_active);
|
2011-12-13 02:43:41 +00:00
|
|
|
}
|
2011-12-30 19:56:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the names of the unclosed/active blocks.
|
|
|
|
*
|
|
|
|
* @return array An array of unclosed blocks.
|
|
|
|
*/
|
|
|
|
public function unclosed() {
|
|
|
|
return $this->_active;
|
|
|
|
}
|
2012-03-03 22:10:12 +00:00
|
|
|
|
2011-09-30 02:11:15 +00:00
|
|
|
}
|