tags) for the layout
*
* @var array
* @access private
*/
var $__scripts = array();
/**
* Holds an array of paths.
*
* @var array
*/
var $__paths = array();
/**
* Constructor
*
* @return View
*/
function __construct(&$controller, $register = true) {
if (is_object($controller)) {
$count = count($this->__passedVars);
for ($j = 0; $j < $count; $j++) {
$var = $this->__passedVars[$j];
$this->{$var} = $controller->{$var};
}
}
parent::__construct();
if ($register) {
ClassRegistry::addObject('view', $this);
}
}
/**
* 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.
*
* @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)
* Special params:
* cache - enable caching for this element accepts boolean or strtotime compatible string.
* Can also be an array
* if an array,'time' is used to specify duration of cache. 'key' can be used to
* create unique cache files.
*
* @return string Rendered Element
* @access 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;
} elseif (file_exists($path . 'elements' . DS . $name . '.thtml')) {
$file = $path . 'elements' . DS . $name . '.thtml';
break;
}
}
if (is_file($file)) {
$params = array_merge_recursive($params, $this->loaded);
$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() > 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
*/
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)) {
if (substr($viewFileName, -3) === 'ctp' || substr($viewFileName, -5) === 'thtml') {
$out = View::_render($viewFileName, $this->viewVars);
} else {
$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->loaded['cache']) &&
(($this->cacheAction != false)) && (Configure::read('Cache.check') === true)
);
if ($isCached) {
$replace = array('
%s", true); trigger_error(sprintf($msg, $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 - contains page title * content_for_layout - contains rendered view file * scripts_for_layout - contains scripts added to header * cakeDebug - if debug is on, cake debug information is added. * * @param string $content_for_layout Content to render in a view, wrapped by the surrounding layout. * @return mixed Rendered output, or false on error */ function renderLayout($content_for_layout, $layout = null) { $layoutFileName = $this->_getLayoutFileName($layout); $debug = ''; if (isset($this->viewVars['cakeDebug']) && Configure::read() > 2) { $params = array('controller' => $this->viewVars['cakeDebug']); $debug = View::element('dump', $params, false); unset($this->viewVars['cakeDebug']); } if ($this->pageTitle !== false) { $pageTitle = $this->pageTitle; } else { $pageTitle = Inflector::humanize($this->viewPath); } $data_for_layout = array_merge($this->viewVars, array( 'title_for_layout' => $pageTitle, 'content_for_layout' => $content_for_layout, 'scripts_for_layout' => join("\n\t", $this->__scripts), 'cakeDebug' => $debug )); if (empty($this->loaded) && !empty($this->helpers)) { $loadHelpers = true; } else { $loadHelpers = false; $data_for_layout = array_merge($data_for_layout, $this->loaded); } if (!empty($this->loaded)) { $helpers = array_keys($this->loaded); foreach ($helpers as $helperName) { $helper =& $this->loaded[$helperName]; if (is_object($helper)) { if (is_subclass_of($helper, 'Helper') || is_subclass_of($helper, 'helper')) { $helper->beforeLayout(); } } } } if (substr($layoutFileName, -3) === 'ctp' || substr($layoutFileName, -5) === 'thtml') { $this->output = View::_render($layoutFileName, $data_for_layout, $loadHelpers, true); } else { $this->output = $this->_render($layoutFileName, $data_for_layout, $loadHelpers); } if ($this->output === false) { $this->output = $this->_render($layoutFileName, $data_for_layout); $msg = __("Error in layout %s, got:
%s", true); trigger_error(sprintf($msg, $layoutFileName, $this->output), E_USER_ERROR); return false; } if (!empty($this->loaded)) { $helpers = array_keys($this->loaded); foreach ($helpers as $helperName) { $helper =& $this->loaded[$helperName]; if (is_object($helper)) { if (is_subclass_of($helper, 'Helper') || is_subclass_of($helper, 'helper')) { $helper->afterLayout(); } } } } return $this->output; } /** * Render cached view * * @param string $filename the cache file to include * @param string $timeStart the page render start time */ function renderCache($filename, $timeStart) { ob_start(); include ($filename); if (Configure::read() > 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'); } echo str_replace('', '', $out); return true; } } } /** * Returns a list of variables available in the current View context * * @return array * @access public */ function getVars() { return array_keys($this->viewVars); } /** * Returns the contents of the given View variable(s) * * @return array * @access 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