2005-05-22 23:24:09 +00:00
|
|
|
<?PHP
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// + $Id$
|
|
|
|
// +------------------------------------------------------------------+ //
|
|
|
|
// + Cake <https://developers.nextco.com/cake/> + //
|
|
|
|
// + Copyright: (c) 2005, Cake Authors/Developers + //
|
|
|
|
// + Author(s): Michal Tatarynowicz aka Pies <tatarynowicz@gmail.com> + //
|
|
|
|
// + Larry E. Masters aka PhpNut <nut@phpnut.com> + //
|
|
|
|
// + Kamil Dzielinski aka Brego <brego.dk@gmail.com> + //
|
|
|
|
// +------------------------------------------------------------------+ //
|
|
|
|
// + Licensed under The MIT License + //
|
|
|
|
// + Redistributions of files must retain the above copyright notice. + //
|
|
|
|
// + See: http://www.opensource.org/licenses/mit-license.php + //
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purpose: Renderer
|
|
|
|
* Templating for Controller class.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @author Cake Authors/Developers
|
|
|
|
* @copyright Copyright (c) 2005, Cake Authors/Developers
|
|
|
|
* @link https://developers.nextco.com/cake/wiki/Authors Authors/Developers
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since Cake v 0.2.9
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
uses('object');
|
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Templating for Controller class. Takes care of rendering views.
|
2005-05-22 23:24:09 +00:00
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since Cake v 0.2.9
|
|
|
|
*/
|
2005-06-18 23:26:35 +00:00
|
|
|
class Template extends Object
|
|
|
|
{
|
2005-05-22 23:24:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $base = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @var string
|
2005-05-22 23:24:09 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $layout = 'default';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @var boolean
|
2005-05-22 23:24:09 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $autoRender = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @var boolean
|
2005-05-22 23:24:09 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $autoLayout = true;
|
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Variables for the view
|
2005-05-22 23:24:09 +00:00
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @var array
|
2005-05-22 23:24:09 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $_view_vars = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @var boolean
|
2005-05-22 23:24:09 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2005-06-12 20:50:12 +00:00
|
|
|
var $pageTitle = false;
|
2005-05-22 23:24:09 +00:00
|
|
|
|
|
|
|
/**
|
2005-06-11 03:45:31 +00:00
|
|
|
* Choose the layout to be used when rendering.
|
2005-05-22 23:24:09 +00:00
|
|
|
*
|
2005-06-11 03:45:31 +00:00
|
|
|
* @param string $layout
|
2005-05-22 23:24:09 +00:00
|
|
|
*/
|
|
|
|
function setLayout ($layout) {
|
|
|
|
$this->layout = $layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-11 03:45:31 +00:00
|
|
|
* Saves a variable to use inside a template.
|
2005-05-22 23:24:09 +00:00
|
|
|
*
|
2005-06-11 03:45:31 +00:00
|
|
|
* @param mixed $one A string or an array of data.
|
|
|
|
* @param string $two Value in case $one is a string (which then works as the key), otherwise unused.
|
2005-05-22 23:24:09 +00:00
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function set($one, $two=null) {
|
|
|
|
return $this->_setArray(is_array($one)? $one: array($one=>$two));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Set the title element of the page.
|
2005-05-22 23:24:09 +00:00
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @param string $pageTitle Text for the title
|
2005-05-22 23:24:09 +00:00
|
|
|
*/
|
2005-06-02 19:37:06 +00:00
|
|
|
function setTitle ($pageTitle) {
|
2005-06-12 20:50:12 +00:00
|
|
|
$this->pageTitle = $pageTitle;
|
2005-05-22 23:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Sets data for this view. Will set title is the key "title" is in given $data array.
|
2005-05-22 23:24:09 +00:00
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @param array $data Array of
|
2005-05-22 23:24:09 +00:00
|
|
|
*/
|
|
|
|
function _setArray($data) {
|
|
|
|
foreach ($data as $name => $value) {
|
|
|
|
if ($name == 'title')
|
|
|
|
$this->setTitle ($value);
|
|
|
|
else
|
|
|
|
$this->_view_vars[$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Displays a flash message. A flash message is feedback to the user that displays after editing actions, among other things.
|
2005-05-22 23:24:09 +00:00
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @param string $message Text to display to the user
|
|
|
|
* @param string $url URL fragment
|
|
|
|
* @param int $time Display time, in seconds
|
2005-05-22 23:24:09 +00:00
|
|
|
*/
|
|
|
|
function flash ($message, $url, $time=1) {
|
|
|
|
$this->autoRender = false;
|
|
|
|
$this->autoLayout = false;
|
|
|
|
|
|
|
|
$this->set('url', $this->base.$url);
|
|
|
|
$this->set('message', $message);
|
|
|
|
$this->set('time', $time);
|
|
|
|
|
|
|
|
$this->render(null,false,VIEWS.'layouts'.DS.'flash.thtml');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Render view for given action and layout. If $file is given, that is used
|
|
|
|
* for a view filename (e.g. customFunkyView.thtml).
|
2005-05-22 23:24:09 +00:00
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @param string $action Name of action to render for
|
|
|
|
* @param string $layout
|
|
|
|
* @param string $file Custom filename for view
|
2005-05-22 23:24:09 +00:00
|
|
|
*/
|
|
|
|
function render ($action=null, $layout=null, $file=null) {
|
|
|
|
$this->autoRender = false;
|
|
|
|
|
|
|
|
if (!$action) $action = $this->action;
|
|
|
|
if ($layout) $this->setLayout($layout);
|
|
|
|
|
|
|
|
$view_fn = $file? $file: $this->_getViewFn($action);
|
|
|
|
|
|
|
|
if (!is_file($view_fn)) {
|
|
|
|
DEBUG? trigger_error (sprintf(ERROR_NO_VIEW, $action, $view_fn), E_USER_ERROR)
|
|
|
|
: $this->error('404', 'Not found', sprintf(ERROR_404, '', "missing view \"{$action}\""));
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
$out = $this->_render($view_fn, $this->_view_vars, 0);
|
|
|
|
|
|
|
|
if ($out !== false) {
|
|
|
|
if ($this->layout && $this->autoLayout)
|
|
|
|
$out = $this->renderLayout($out);
|
|
|
|
print $out;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$out = $this->_render($view_fn, $this->_view_vars, false);
|
|
|
|
trigger_error (sprintf(ERROR_IN_VIEW, $view_fn, $out), E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Enter description here... Renders a layout. Returns output from _render(). Returns false on error.
|
2005-05-22 23:24:09 +00:00
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @param string $content_for_layout Content to render in a view
|
|
|
|
* @return string Rendered output
|
2005-05-22 23:24:09 +00:00
|
|
|
*/
|
|
|
|
function renderLayout ($content_for_layout) {
|
|
|
|
$layout_fn = $this->_getLayoutFn();
|
|
|
|
|
|
|
|
$data_for_layout = array_merge($this->_view_vars, array(
|
2005-06-12 20:50:12 +00:00
|
|
|
'title_for_layout'=>$this->pageTitle !== false? $this->pageTitle: Inflector::humanize($this->viewpath),
|
2005-05-22 23:24:09 +00:00
|
|
|
'content_for_layout'=>$content_for_layout));
|
|
|
|
|
|
|
|
if (is_file($layout_fn)) {
|
|
|
|
$out = $this->_render($layout_fn, $data_for_layout);
|
|
|
|
|
|
|
|
if ($out === false) {
|
|
|
|
$out = $this->_render($layout_fn, $data_for_layout, false);
|
|
|
|
trigger_error (sprintf(ERROR_IN_LAYOUT, $layout_fn, $out), E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
trigger_error (sprintf(ERROR_NO_LAYOUT, $this->layout, $layout_fn), E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-29 19:43:59 +00:00
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
|
2005-05-29 19:43:59 +00:00
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @param string $name Name of template file
|
|
|
|
* @param array $params Array of data for rendered view
|
|
|
|
* @return string Rendered output
|
2005-05-29 19:43:59 +00:00
|
|
|
*/
|
|
|
|
function renderElement ($name, $params=array()) {
|
|
|
|
$fn = ELEMENTS.$name.'.thtml';
|
|
|
|
|
|
|
|
if (!file_exists($fn))
|
|
|
|
return "(Error rendering {$name})";
|
|
|
|
|
2005-06-14 19:57:01 +00:00
|
|
|
return $this->_render($fn, array_merge($this->_view_vars, $params));
|
2005-05-29 19:43:59 +00:00
|
|
|
}
|
|
|
|
|
2005-05-22 23:24:09 +00:00
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Returns layout filename for this template as a string.
|
2005-05-22 23:24:09 +00:00
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @return string Filename for layout file (.thtml).
|
2005-05-22 23:24:09 +00:00
|
|
|
*/
|
|
|
|
function _getLayoutFn() {
|
|
|
|
return VIEWS."layouts".DS."{$this->layout}.thtml";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Returns filename of given action's template file (.thtml) as a string.
|
2005-05-22 23:24:09 +00:00
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @param string $action Controller action to find template filename for
|
|
|
|
* @return string Template filename
|
2005-05-22 23:24:09 +00:00
|
|
|
*/
|
|
|
|
function _getViewFn($action) {
|
2005-05-29 19:43:59 +00:00
|
|
|
return VIEWS.$this->viewpath.DS."{$action}.thtml";
|
2005-05-22 23:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Renders and returns output for given view filename with its
|
|
|
|
* array of data.
|
2005-05-22 23:24:09 +00:00
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @param string $___view_fn Filename of the view
|
|
|
|
* @param array $___data_for_view Data to include in rendered view
|
|
|
|
* @param boolean $___play_safe If set to false, the include() of the $__view_fn is done without suppressing output of errors
|
|
|
|
* @return string Rendered output
|
2005-05-22 23:24:09 +00:00
|
|
|
*/
|
2005-06-11 03:45:31 +00:00
|
|
|
function _render($___view_fn, $___data_for_view, $___play_safe = true)
|
|
|
|
{
|
2005-05-22 23:24:09 +00:00
|
|
|
extract($___data_for_view, EXTR_SKIP); # load all view variables
|
|
|
|
$BASE = $this->base;
|
|
|
|
$params = &$this->params;
|
2005-06-12 20:50:12 +00:00
|
|
|
$page_title = $this->pageTitle;
|
2005-05-22 23:24:09 +00:00
|
|
|
ob_start(); # start caching output (eval outputs directly so we need to cache)
|
|
|
|
|
|
|
|
# include the template
|
|
|
|
$___play_safe? @include($___view_fn): include($___view_fn);
|
|
|
|
|
|
|
|
$out = ob_get_contents(); # retrieve cached output
|
|
|
|
ob_end_clean(); # end caching output
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Returns given string trimmed to given length, adding an elipsis '..' if necessary.
|
2005-05-22 23:24:09 +00:00
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @param string $string String to trim
|
|
|
|
* @param int $length Length of returned string, excluding ellipsis
|
|
|
|
* @return string Trimmed string
|
2005-05-22 23:24:09 +00:00
|
|
|
*/
|
|
|
|
function trimTo ($string, $length) {
|
|
|
|
return substr($string, 0, $length).(strlen($string)>$length? '..': null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-15 21:41:38 +00:00
|
|
|
?>
|