tags) for the layout
*
* @var array
* @access protected
*/
protected $_scripts = array();
/**
* Holds an array of paths.
*
* @var array
* @access private
*/
private $__paths = array();
/**
* Constructor
*
* @param Controller $controller A controller object to pull View::__passedArgs from.
*/
function __construct(&$controller) {
if (is_object($controller)) {
$count = count($this->__passedVars);
for ($j = 0; $j < $count; $j++) {
$var = $this->__passedVars[$j];
$this->{$var} = $controller->{$var};
}
}
$this->Helpers = new HelperCollection($this);
parent::__construct();
}
/**
* Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
*
* This realizes the concept of Elements, (or "partial layouts")
* and the $params array is used to send data to be used in the
* Element. Elements can be cached through use of the cache key.
*
* ### Special params
*
* - `cache` - enable caching for this element accepts boolean or strtotime compatible string.
* Can also be an array. If `cache` is an array,
* `time` is used to specify duration of cache.
* `key` can be used to create unique cache files.
* - `plugin` - Load an element from a specific plugin.
*
* @param string $name Name of template file in the/app/views/elements/ folder
* @param array $params Array of data to be made available to the for rendered
* view (i.e. the Element)
* @return string Rendered Element
*/
public function element($name, $params = array(), $loadHelpers = false) {
$file = $plugin = $key = null;
if (isset($params['plugin'])) {
$plugin = $params['plugin'];
}
if (isset($this->plugin) && !$plugin) {
$plugin = $this->plugin;
}
if (isset($params['cache'])) {
$expires = '+1 day';
if (is_array($params['cache'])) {
$expires = $params['cache']['time'];
$key = Inflector::slug($params['cache']['key']);
} elseif ($params['cache'] !== true) {
$expires = $params['cache'];
$key = implode('_', array_keys($params));
}
if ($expires) {
$cacheFile = 'element_' . $key . '_' . $plugin . Inflector::slug($name);
$cache = cache('views' . DS . $cacheFile, null, $expires);
if (is_string($cache)) {
return $cache;
}
}
}
$paths = $this->_paths($plugin);
foreach ($paths as $path) {
if (file_exists($path . 'elements' . DS . $name . $this->ext)) {
$file = $path . 'elements' . DS . $name . $this->ext;
break;
}
}
if (is_file($file)) {
$element = $this->_render($file, array_merge($this->viewVars, $params), $loadHelpers);
if (isset($params['cache']) && isset($cacheFile) && isset($expires)) {
cache('views' . DS . $cacheFile, $element, $expires);
}
return $element;
}
$file = $paths[0] . 'elements' . DS . $name . $this->ext;
if (Configure::read('debug') > 0) {
return "Not Found: " . $file;
}
}
/**
* Renders view for given action and layout. If $file is given, that is used
* for a view filename (e.g. customFunkyView.ctp).
*
* @param string $action Name of action to render for
* @param string $layout Layout to use
* @param string $file Custom filename for view
* @return string Rendered Element
*/
public function render($action = null, $layout = null, $file = null) {
if ($this->hasRendered) {
return true;
}
$out = null;
if ($file != null) {
$action = $file;
}
if ($action !== false && $viewFileName = $this->_getViewFileName($action)) {
$out = $this->_render($viewFileName, $this->viewVars);
}
if ($layout === null) {
$layout = $this->layout;
}
if ($out !== false) {
if ($layout && $this->autoLayout) {
$out = $this->renderLayout($out, $layout);
$isCached = (
isset($this->Helpers->Cache) &&
(($this->cacheAction != false)) && (Configure::read('Cache.check') === true)
);
if ($isCached) {
$replace = array('
%s"), $viewFileName, $out), E_USER_ERROR); } return $out; } /** * Renders a layout. Returns output from _render(). Returns false on error. * Several variables are created for use in layout. * * - `title_for_layout` - A backwards compatible place holder, you should set this value if you want more control. * - `content_for_layout` - contains rendered view file * - `scripts_for_layout` - contains scripts added to header * * @param string $content_for_layout Content to render in a view, wrapped by the surrounding layout. * @return mixed Rendered output, or false on error */ public function renderLayout($content_for_layout, $layout = null) { $layoutFileName = $this->_getLayoutFileName($layout); if (empty($layoutFileName)) { return $this->output; } $dataForLayout = array_merge($this->viewVars, array( 'content_for_layout' => $content_for_layout, 'scripts_for_layout' => implode("\n\t", $this->_scripts), )); if (!isset($dataForLayout['title_for_layout'])) { $dataForLayout['title_for_layout'] = Inflector::humanize($this->viewPath); } $attached = $this->Helpers->attached(); if (empty($attached) && !empty($this->helpers)) { $loadHelpers = true; } else { $loadHelpers = false; $dataForLayout = array_merge($dataForLayout); } $this->Helpers->trigger('beforeLayout', array(&$this)); $this->output = $this->_render($layoutFileName, $dataForLayout, $loadHelpers, true); if ($this->output === false) { $this->output = $this->_render($layoutFileName, $data_for_layout); trigger_error(sprintf(__("Error in layout %s, got:
%s"), $layoutFileName, $this->output), E_USER_ERROR); return false; } $this->Helpers->trigger('afterLayout', array(&$this)); return $this->output; } /** * Render cached view. Works in concert with CacheHelper and Dispatcher to * render cached view files. * * @param string $filename the cache file to include * @param string $timeStart the page render start time * @return boolean Success of rendering the cached file. */ public function renderCache($filename, $timeStart) { ob_start(); include ($filename); if (Configure::read('debug') > 0 && $this->layout != 'xml') { echo ""; } $out = ob_get_clean(); if (preg_match('/^/', $out, $match)) { if (time() >= $match['1']) { @unlink($filename); unset ($out); return false; } else { if ($this->layout === 'xml') { header('Content-type: text/xml'); } $commentLength = strlen(''); echo substr($out, $commentLength); return true; } } } /** * Returns a list of variables available in the current View context * * @return array Array of the set view variable names. */ public function getVars() { return array_keys($this->viewVars); } /** * Returns the contents of the given View variable(s) * * @param string $var The view var you want the contents of. * @return mixed The content of the named var if its set, otherwise null. */ public function getVar($var) { if (!isset($this->viewVars[$var])) { return null; } else { return $this->viewVars[$var]; } } /** * Adds a script block or other element to be inserted in $scripts_for_layout in * the `