tags) for the layout
*
* @var array
* @access private
*/
var $__scripts = array();
/**
* Holds an array of paths.
*
* @var array
* @access private
*/
var $__paths = array();
/**
* Constructor
*
* @param Controller $controller A controller object to pull View::__passedArgs from.
* @param boolean $register Should the View instance be registered in the ClassRegistry
* @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.
*
* ### 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
* @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);
$exts = $this->_getExtensions();
foreach ($exts as $ext) {
foreach ($paths as $path) {
if (file_exists($path . 'elements' . DS . $name . $ext)) {
$file = $path . 'elements' . DS . $name . $ext;
break;
}
}
}
if (is_file($file)) {
$vars = array_merge($this->viewVars, $params);
foreach ($this->loaded as $name => $helper) {
if (!isset($vars[$name])) {
$vars[$name] =& $this->loaded[$name];
}
}
$element = $this->_render($file, $vars, $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
* @access 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->loaded['cache']) ||
Configure::read('Cache.check') === true
);
if ($isCached) {
$replace = array('
%s", true), $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 * @access 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); } if (empty($this->loaded) && !empty($this->helpers)) { $loadHelpers = true; } else { $loadHelpers = false; $dataForLayout = array_merge($dataForLayout, $this->loaded); } $this->_triggerHelpers('beforeLayout'); $this->output = $this->_render($layoutFileName, $dataForLayout, $loadHelpers, true); if ($this->output === false) { $this->output = $this->_render($layoutFileName, $dataForLayout); trigger_error(sprintf(__("Error in layout %s, got:
%s", true), $layoutFileName, $this->output), E_USER_ERROR); return false; } $this->_triggerHelpers('afterLayout'); return $this->output; } /** * Fire a callback on all loaded Helpers. All helpers must implement this method, * it is not checked before being called. You can add additional helper callbacks in AppHelper. * * @param string $callback name of callback fire. * @access protected * @return void */ function _triggerHelpers($callback) { if (empty($this->loaded)) { return false; } $helpers = array_keys($this->loaded); foreach ($helpers as $helperName) { $helper =& $this->loaded[$helperName]; if (is_object($helper)) { if (is_subclass_of($helper, 'Helper')) { $helper->{$callback}(); } } } } /** * 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. * @access public */ 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'); } $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. * @access 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. * @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 `