2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Methods for displaying presentation data in the view.
|
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-03 23:07:21 +00:00
|
|
|
App::uses('HelperCollection', 'View');
|
2011-09-07 20:46:59 +00:00
|
|
|
App::uses('AppHelper', 'View/Helper');
|
2010-12-11 05:47:55 +00:00
|
|
|
App::uses('Router', 'Routing');
|
2011-09-30 02:11:15 +00:00
|
|
|
App::uses('ViewBlock', 'View');
|
2011-12-25 23:30:19 +00:00
|
|
|
App::uses('CakeEvent', 'Event');
|
|
|
|
App::uses('CakeEventManager', 'Event');
|
2012-02-06 15:15:00 +00:00
|
|
|
App::uses('CakeResponse', 'Network');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-12-22 02:59:25 +00:00
|
|
|
* View, the V in the MVC triad. View interacts with Helpers and view variables passed
|
2012-12-22 22:48:15 +00:00
|
|
|
* in from the controller to render the results of the controller action. Often this is HTML,
|
2010-12-22 02:59:25 +00:00
|
|
|
* but can also take the form of JSON, XML, PDF's or streaming files.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* CakePHP uses a two-step-view pattern. This means that the view content is rendered first,
|
|
|
|
* and then inserted into the selected layout. This also means you can pass data from the view to the
|
2010-12-22 02:59:25 +00:00
|
|
|
* layout using `$this->set()`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* Since 2.1, the base View class also includes support for themes by default. Theme views are regular
|
|
|
|
* view files that can provide unique HTML and static assets. If theme views are not found for the
|
|
|
|
* current view the default app view files will be used. You can set `$this->theme = 'mytheme'`
|
2012-01-22 20:45:26 +00:00
|
|
|
* in your Controller to use the Themes.
|
|
|
|
*
|
|
|
|
* Example of theme path with `$this->theme = 'SuperHot';` Would be `app/View/Themed/SuperHot/Posts`
|
2011-12-22 02:57:18 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View
|
2011-08-01 00:28:35 +00:00
|
|
|
* @property CacheHelper $Cache
|
|
|
|
* @property FormHelper $Form
|
|
|
|
* @property HtmlHelper $Html
|
|
|
|
* @property JsHelper $Js
|
|
|
|
* @property NumberHelper $Number
|
|
|
|
* @property PaginatorHelper $Paginator
|
|
|
|
* @property RssHelper $Rss
|
|
|
|
* @property SessionHelper $Session
|
|
|
|
* @property TextHelper $Text
|
|
|
|
* @property TimeHelper $Time
|
2011-12-13 02:43:41 +00:00
|
|
|
* @property ViewBlock $Blocks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2016-04-08 12:33:26 +00:00
|
|
|
class View extends CakeObject {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-03 03:50:41 +00:00
|
|
|
/**
|
|
|
|
* Helpers collection
|
|
|
|
*
|
|
|
|
* @var HelperCollection
|
|
|
|
*/
|
|
|
|
public $Helpers;
|
2010-07-15 03:14:39 +00:00
|
|
|
|
2011-09-30 02:11:15 +00:00
|
|
|
/**
|
|
|
|
* ViewBlock instance.
|
|
|
|
*
|
|
|
|
* @var ViewBlock
|
|
|
|
*/
|
|
|
|
public $Blocks;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Name of the plugin.
|
|
|
|
*
|
2010-04-24 04:22:21 +00:00
|
|
|
* @link http://manual.cakephp.org/chapter/plugins
|
2008-05-30 11:40:08 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $plugin = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Name of the controller.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var string
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Current passed params
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $passedArgs = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* An array of names of built-in helpers to include.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var mixed
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-09-02 01:39:50 +00:00
|
|
|
public $helpers = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Path to View.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var string
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $viewPath = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Variables for the view
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $viewVars = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-02-12 20:54:39 +00:00
|
|
|
/**
|
|
|
|
* Name of view to use with this View.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $view = null;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2018-10-22 08:11:01 +00:00
|
|
|
* Name of layout to use with this View. If `false` then no layout is rendered.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2018-10-22 08:11:01 +00:00
|
|
|
* @var string|bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $layout = 'default';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Path to Layout.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var string
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $layoutPath = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-09-27 17:36:43 +00:00
|
|
|
* Turns on or off CakePHP's conventional mode of applying layout files. On by default.
|
2010-12-22 02:59:25 +00:00
|
|
|
* Setting to off means that layouts will not be automatically applied to rendered views.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $autoLayout = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-09-27 17:36:43 +00:00
|
|
|
* File extension. Defaults to CakePHP's template ".ctp".
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $ext = '.ctp';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Sub-directory for this view file. This is often used for extension based routing.
|
2011-10-27 12:10:57 +00:00
|
|
|
* Eg. With an `xml` extension, $subDir would be `xml/`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $subDir = null;
|
2011-02-12 20:54:39 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-01-23 09:57:10 +00:00
|
|
|
* Theme name.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $theme = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to define methods a controller that will be cached.
|
|
|
|
*
|
|
|
|
* @see Controller::$cacheAction
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $cacheAction = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-10-27 12:10:57 +00:00
|
|
|
* Holds current errors for the model validation.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $validationErrors = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* True when the view has been rendered.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $hasRendered = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-10-27 12:10:57 +00:00
|
|
|
* List of generated DOM UUIDs.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $uuids = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-11 03:55:42 +00:00
|
|
|
/**
|
|
|
|
* An instance of a CakeRequest object that contains information about the current request.
|
|
|
|
* This object contains all the information about a request and several methods for reading
|
2011-02-12 20:54:39 +00:00
|
|
|
* additional information about the request.
|
2010-09-11 03:55:42 +00:00
|
|
|
*
|
|
|
|
* @var CakeRequest
|
|
|
|
*/
|
|
|
|
public $request;
|
|
|
|
|
2012-02-06 15:15:00 +00:00
|
|
|
/**
|
|
|
|
* Reference to the Response object
|
|
|
|
*
|
|
|
|
* @var CakeResponse
|
|
|
|
*/
|
|
|
|
public $response;
|
|
|
|
|
2010-11-10 03:16:45 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* The Cache configuration View will use to store cached elements. Changing this will change
|
|
|
|
* the default configuration elements are stored under. You can also choose a cache config
|
2010-11-10 03:16:45 +00:00
|
|
|
* per element.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @see View::element()
|
|
|
|
*/
|
|
|
|
public $elementCache = 'default';
|
|
|
|
|
2012-07-04 09:48:10 +00:00
|
|
|
/**
|
|
|
|
* Element cache settings
|
|
|
|
*
|
2012-11-28 22:30:47 +00:00
|
|
|
* @var array
|
2012-07-04 09:48:10 +00:00
|
|
|
* @see View::_elementCache();
|
|
|
|
* @see View::_renderElement
|
|
|
|
*/
|
|
|
|
public $elementCacheSettings = array();
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-10-27 12:10:57 +00:00
|
|
|
* List of variables to collect from the associated controller.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_passedVars = array(
|
2012-01-23 09:57:10 +00:00
|
|
|
'viewVars', 'autoLayout', 'ext', 'helpers', 'view', 'layout', 'name', 'theme',
|
2010-09-11 03:55:42 +00:00
|
|
|
'layoutPath', 'viewPath', 'request', 'plugin', 'passedArgs', 'cacheAction'
|
2008-11-09 03:20:14 +00:00
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-10-27 12:10:57 +00:00
|
|
|
* Scripts (and/or other <head /> tags) for the layout.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-05 11:15:17 +00:00
|
|
|
protected $_scripts = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds an array of paths.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_paths = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2014-04-19 20:21:30 +00:00
|
|
|
/**
|
|
|
|
* Holds an array of plugin paths.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_pathsForPlugin = array();
|
|
|
|
|
2011-09-23 01:07:21 +00:00
|
|
|
/**
|
|
|
|
* The names of views and their parents used with View::extend();
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_parents = array();
|
|
|
|
|
|
|
|
/**
|
2011-09-30 02:22:03 +00:00
|
|
|
* The currently rendering view file. Used for resolving parent files.
|
2011-09-23 01:07:21 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_current = null;
|
|
|
|
|
2011-09-26 01:52:11 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Currently rendering an element. Used for finding parent fragments
|
2011-09-26 01:52:11 +00:00
|
|
|
* for elements.
|
|
|
|
*
|
2011-09-30 02:22:03 +00:00
|
|
|
* @var string
|
2011-09-26 01:52:11 +00:00
|
|
|
*/
|
2011-09-30 02:22:03 +00:00
|
|
|
protected $_currentType = '';
|
2011-09-26 01:52:11 +00:00
|
|
|
|
2011-09-23 01:07:21 +00:00
|
|
|
/**
|
|
|
|
* Content stack, used for nested templates that all use View::extend();
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_stack = array();
|
|
|
|
|
2011-12-25 23:30:19 +00:00
|
|
|
/**
|
|
|
|
* Instance of the CakeEventManager this View object is using
|
|
|
|
* to dispatch inner events. Usually the manager is shared with
|
|
|
|
* the controller, so it it possible to register view events in
|
|
|
|
* the controller layer.
|
|
|
|
*
|
|
|
|
* @var CakeEventManager
|
|
|
|
*/
|
|
|
|
protected $_eventManager = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the event manager was already configured for this object
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2011-12-25 23:30:19 +00:00
|
|
|
*/
|
|
|
|
protected $_eventManagerConfigured = false;
|
|
|
|
|
2012-11-28 22:30:47 +00:00
|
|
|
/**
|
|
|
|
* Constant for view file type 'view'
|
2014-02-07 14:45:00 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2012-11-28 22:30:47 +00:00
|
|
|
*/
|
2011-09-30 02:22:03 +00:00
|
|
|
const TYPE_VIEW = 'view';
|
2012-11-28 22:30:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constant for view file type 'element'
|
2014-02-07 14:45:00 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2012-11-28 22:30:47 +00:00
|
|
|
*/
|
2011-09-30 02:22:03 +00:00
|
|
|
const TYPE_ELEMENT = 'element';
|
2012-11-28 22:30:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constant for view file type 'layout'
|
2014-02-07 14:45:00 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2012-11-28 22:30:47 +00:00
|
|
|
*/
|
2011-09-30 02:22:03 +00:00
|
|
|
const TYPE_LAYOUT = 'layout';
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2011-10-27 12:16:38 +00:00
|
|
|
* @param Controller $controller A controller object to pull View::_passedVars from.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2024-07-24 19:55:01 +00:00
|
|
|
public function __construct(?Controller $controller = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_object($controller)) {
|
2011-08-20 04:43:34 +00:00
|
|
|
$count = count($this->_passedVars);
|
2008-05-30 11:40:08 +00:00
|
|
|
for ($j = 0; $j < $count; $j++) {
|
2011-08-20 04:43:34 +00:00
|
|
|
$var = $this->_passedVars[$j];
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->{$var} = $controller->{$var};
|
|
|
|
}
|
2011-12-25 23:30:19 +00:00
|
|
|
$this->_eventManager = $controller->getEventManager();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-06-04 13:29:28 +00:00
|
|
|
if (empty($this->request) && !($this->request = Router::getRequest(true))) {
|
|
|
|
$this->request = new CakeRequest(null, false);
|
|
|
|
$this->request->base = '';
|
|
|
|
$this->request->here = $this->request->webroot = '/';
|
|
|
|
}
|
2012-02-06 15:15:00 +00:00
|
|
|
if (is_object($controller) && isset($controller->response)) {
|
|
|
|
$this->response = $controller->response;
|
|
|
|
} else {
|
2012-09-27 18:28:19 +00:00
|
|
|
$this->response = new CakeResponse();
|
2012-02-06 15:15:00 +00:00
|
|
|
}
|
2010-07-03 03:50:41 +00:00
|
|
|
$this->Helpers = new HelperCollection($this);
|
2011-09-30 02:11:15 +00:00
|
|
|
$this->Blocks = new ViewBlock();
|
2013-08-27 02:43:28 +00:00
|
|
|
$this->loadHelpers();
|
2008-05-30 11:40:08 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-12-25 23:30:19 +00:00
|
|
|
/**
|
|
|
|
* Returns the CakeEventManager manager instance that is handling any callbacks.
|
|
|
|
* You can use this instance to register any new listeners or callbacks to the
|
|
|
|
* controller events, or create your own events and trigger them at will.
|
|
|
|
*
|
|
|
|
* @return CakeEventManager
|
|
|
|
*/
|
|
|
|
public function getEventManager() {
|
2012-05-15 01:18:09 +00:00
|
|
|
if (empty($this->_eventManager)) {
|
2011-12-25 23:30:19 +00:00
|
|
|
$this->_eventManager = new CakeEventManager();
|
2012-05-15 01:18:09 +00:00
|
|
|
}
|
|
|
|
if (!$this->_eventManagerConfigured) {
|
2011-12-25 23:30:19 +00:00
|
|
|
$this->_eventManager->attach($this->Helpers);
|
|
|
|
$this->_eventManagerConfigured = true;
|
|
|
|
}
|
|
|
|
return $this->_eventManager;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
|
|
|
|
*
|
2011-04-17 01:26:33 +00:00
|
|
|
* 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 improving performance by using the `cache` option.
|
2009-08-20 05:20:13 +00:00
|
|
|
*
|
2011-11-13 02:48:26 +00:00
|
|
|
* @param string $name Name of template file in the/app/View/Elements/ folder,
|
2012-12-22 22:48:15 +00:00
|
|
|
* or `MyPlugin.template` to use the template element from MyPlugin. If the element
|
2011-12-22 02:57:18 +00:00
|
|
|
* is not found in the plugin, the normal view path cascade will be searched.
|
2011-04-17 01:26:33 +00:00
|
|
|
* @param array $data Array of data to be made available to the rendered view (i.e. the Element)
|
|
|
|
* @param array $options Array of options. Possible keys are:
|
2010-11-10 03:16:45 +00:00
|
|
|
* - `cache` - Can either be `true`, to enable caching using the config in View::$elementCache. Or an array
|
|
|
|
* If an array, the following keys can be used:
|
|
|
|
* - `config` - Used to store the cached element in a custom cache configuration.
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `key` - Used to define the key used in the Cache::write(). It will be prefixed with `element_`
|
2014-04-30 13:55:27 +00:00
|
|
|
* - `plugin` - (deprecated!) Load an element from a specific plugin. This option is deprecated, and
|
|
|
|
* will be removed in CakePHP 3.0. Use `Plugin.element_name` instead.
|
2011-04-17 01:26:33 +00:00
|
|
|
* - `callbacks` - Set to true to fire beforeRender and afterRender helper callbacks for this element.
|
2010-11-05 03:32:18 +00:00
|
|
|
* Defaults to false.
|
2012-11-30 13:11:45 +00:00
|
|
|
* - `ignoreMissing` - Used to allow missing elements. Set to true to not trigger notices.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Rendered Element
|
|
|
|
*/
|
2011-04-17 01:26:33 +00:00
|
|
|
public function element($name, $data = array(), $options = array()) {
|
2012-07-04 09:48:10 +00:00
|
|
|
$file = $plugin = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-04-17 01:26:33 +00:00
|
|
|
if (isset($options['plugin'])) {
|
2011-12-22 02:57:18 +00:00
|
|
|
$name = Inflector::camelize($options['plugin']) . '.' . $name;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-12-22 02:57:18 +00:00
|
|
|
|
2012-07-04 09:48:10 +00:00
|
|
|
if (!isset($options['callbacks'])) {
|
|
|
|
$options['callbacks'] = false;
|
2011-04-17 01:26:33 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-04-17 01:26:33 +00:00
|
|
|
if (isset($options['cache'])) {
|
2012-07-06 00:13:34 +00:00
|
|
|
$contents = $this->_elementCache($name, $data, $options);
|
2010-11-08 05:05:44 +00:00
|
|
|
if ($contents !== false) {
|
|
|
|
return $contents;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 02:57:18 +00:00
|
|
|
$file = $this->_getElementFilename($name);
|
2010-11-10 03:16:45 +00:00
|
|
|
if ($file) {
|
2012-07-04 09:48:10 +00:00
|
|
|
return $this->_renderElement($file, $data, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-07-04 09:48:10 +00:00
|
|
|
|
2012-11-30 13:11:45 +00:00
|
|
|
if (empty($options['ignoreMissing'])) {
|
2012-12-15 02:32:01 +00:00
|
|
|
list ($plugin, $name) = pluginSplit($name, true);
|
|
|
|
$name = str_replace('/', DS, $name);
|
|
|
|
$file = $plugin . 'Elements' . DS . $name . $this->ext;
|
2012-11-30 13:11:45 +00:00
|
|
|
trigger_error(__d('cake_dev', 'Element Not Found: %s', $file), E_USER_NOTICE);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-11-30 13:11:45 +00:00
|
|
|
/**
|
|
|
|
* Checks if an element exists
|
|
|
|
*
|
2012-12-01 09:34:02 +00:00
|
|
|
* @param string $name Name of template file in the /app/View/Elements/ folder,
|
2012-12-22 22:48:15 +00:00
|
|
|
* or `MyPlugin.template` to check the template element from MyPlugin. If the element
|
2012-11-30 13:11:45 +00:00
|
|
|
* is not found in the plugin, the normal view path cascade will be searched.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2012-11-30 13:11:45 +00:00
|
|
|
*/
|
|
|
|
public function elementExists($name) {
|
|
|
|
return (bool)$this->_getElementFilename($name);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-02-12 20:54:39 +00:00
|
|
|
* Renders view for given view file and layout.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-12-22 02:59:25 +00:00
|
|
|
* Render triggers helper callbacks, which are fired before and after the view are rendered,
|
2012-12-22 22:48:15 +00:00
|
|
|
* as well as before and after the layout. The helper callbacks are called:
|
2010-12-22 02:59:25 +00:00
|
|
|
*
|
|
|
|
* - `beforeRender`
|
|
|
|
* - `afterRender`
|
|
|
|
* - `beforeLayout`
|
|
|
|
* - `afterLayout`
|
|
|
|
*
|
|
|
|
* If View::$autoRender is false and no `$layout` is provided, the view will be returned bare.
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* View and layout names can point to plugin views/layouts. Using the `Plugin.view` syntax
|
|
|
|
* a plugin view/layout can be used instead of the app ones. If the chosen plugin is not found
|
2011-12-22 02:57:18 +00:00
|
|
|
* the view will be located along the regular view path cascade.
|
|
|
|
*
|
2018-11-16 01:41:57 +00:00
|
|
|
* @param false|string $view Name of view file to use.
|
2010-12-22 02:59:25 +00:00
|
|
|
* @param string $layout Layout to use.
|
2014-02-05 21:11:36 +00:00
|
|
|
* @return string|null Rendered content or null if content already rendered and returned earlier.
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers View.beforeRender $this, array($viewFileName)
|
|
|
|
* @triggers View.afterRender $this, array($viewFileName)
|
2013-07-27 04:51:30 +00:00
|
|
|
* @throws CakeException If there is an error in the view.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-02-12 20:54:39 +00:00
|
|
|
public function render($view = null, $layout = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->hasRendered) {
|
2015-09-25 15:11:20 +00:00
|
|
|
return null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-12 20:54:39 +00:00
|
|
|
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$this->_currentType = static::TYPE_VIEW;
|
2011-12-25 23:30:19 +00:00
|
|
|
$this->getEventManager()->dispatch(new CakeEvent('View.beforeRender', $this, array($viewFileName)));
|
2011-12-18 03:53:21 +00:00
|
|
|
$this->Blocks->set('content', $this->_render($viewFileName));
|
2011-12-25 23:30:19 +00:00
|
|
|
$this->getEventManager()->dispatch(new CakeEvent('View.afterRender', $this, array($viewFileName)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($layout === null) {
|
|
|
|
$layout = $this->layout;
|
|
|
|
}
|
2010-11-05 02:36:37 +00:00
|
|
|
if ($layout && $this->autoLayout) {
|
2011-12-18 03:53:21 +00:00
|
|
|
$this->Blocks->set('content', $this->renderLayout('', $layout));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-11-05 02:36:37 +00:00
|
|
|
$this->hasRendered = true;
|
2011-12-18 03:53:21 +00:00
|
|
|
return $this->Blocks->get('content');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Renders a layout. Returns output from _render(). Returns false on error.
|
2008-08-03 05:24:10 +00:00
|
|
|
* Several variables are created for use in layout.
|
2009-08-20 05:15:08 +00:00
|
|
|
*
|
2010-03-28 16:53:57 +00:00
|
|
|
* - `title_for_layout` - A backwards compatible place holder, you should set this value if you want more control.
|
2009-08-20 05:15:08 +00:00
|
|
|
* - `content_for_layout` - contains rendered view file
|
2011-08-17 01:26:54 +00:00
|
|
|
* - `scripts_for_layout` - Contains content added with addScript() as well as any content in
|
2012-12-22 22:48:15 +00:00
|
|
|
* the 'meta', 'css', and 'script' blocks. They are appended in that order.
|
2011-08-17 01:26:54 +00:00
|
|
|
*
|
2011-09-23 01:07:21 +00:00
|
|
|
* Deprecated features:
|
2011-12-29 15:22:08 +00:00
|
|
|
*
|
2011-09-23 01:07:21 +00:00
|
|
|
* - `$scripts_for_layout` is deprecated and will be removed in CakePHP 3.0.
|
2012-12-22 22:48:15 +00:00
|
|
|
* Use the block features instead. `meta`, `css` and `script` will be populated
|
2011-09-23 01:07:21 +00:00
|
|
|
* by the matching methods on HtmlHelper.
|
2014-01-09 15:45:49 +00:00
|
|
|
* - `$title_for_layout` is deprecated and will be removed in CakePHP 3.0.
|
|
|
|
* Use the `title` block instead.
|
2011-12-18 03:14:58 +00:00
|
|
|
* - `$content_for_layout` is deprecated and will be removed in CakePHP 3.0.
|
2011-09-23 01:07:21 +00:00
|
|
|
* Use the `content` block instead.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-10-08 19:14:17 +00:00
|
|
|
* @param string $content Content to render in a view, wrapped by the surrounding layout.
|
2011-07-29 02:45:47 +00:00
|
|
|
* @param string $layout Layout name
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed Rendered output, or false on error
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers View.beforeLayout $this, array($layoutFileName)
|
|
|
|
* @triggers View.afterLayout $this, array($layoutFileName)
|
2010-12-12 00:01:07 +00:00
|
|
|
* @throws CakeException if there is an error in the view.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-10-08 19:14:17 +00:00
|
|
|
public function renderLayout($content, $layout = null) {
|
2008-10-23 00:10:44 +00:00
|
|
|
$layoutFileName = $this->_getLayoutFileName($layout);
|
2009-07-21 01:33:12 +00:00
|
|
|
if (empty($layoutFileName)) {
|
2011-10-08 19:14:17 +00:00
|
|
|
return $this->Blocks->get('content');
|
2009-07-21 01:33:12 +00:00
|
|
|
}
|
2011-10-08 19:14:17 +00:00
|
|
|
|
|
|
|
if (empty($content)) {
|
|
|
|
$content = $this->Blocks->get('content');
|
2013-09-12 22:09:31 +00:00
|
|
|
} else {
|
|
|
|
$this->Blocks->set('content', $content);
|
2011-10-08 19:14:17 +00:00
|
|
|
}
|
2011-12-25 23:30:19 +00:00
|
|
|
$this->getEventManager()->dispatch(new CakeEvent('View.beforeLayout', $this, array($layoutFileName)));
|
2009-07-21 01:33:12 +00:00
|
|
|
|
2011-08-17 01:26:54 +00:00
|
|
|
$scripts = implode("\n\t", $this->_scripts);
|
2011-12-29 16:58:05 +00:00
|
|
|
$scripts .= $this->Blocks->get('meta') . $this->Blocks->get('css') . $this->Blocks->get('script');
|
2011-08-17 01:26:54 +00:00
|
|
|
|
2010-11-01 03:15:34 +00:00
|
|
|
$this->viewVars = array_merge($this->viewVars, array(
|
2011-10-08 19:14:17 +00:00
|
|
|
'content_for_layout' => $content,
|
2011-08-17 01:26:54 +00:00
|
|
|
'scripts_for_layout' => $scripts,
|
2008-11-09 03:37:24 +00:00
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2014-01-09 15:45:49 +00:00
|
|
|
$title = $this->Blocks->get('title');
|
|
|
|
if ($title === '') {
|
|
|
|
if (isset($this->viewVars['title_for_layout'])) {
|
|
|
|
$title = $this->viewVars['title_for_layout'];
|
|
|
|
} else {
|
|
|
|
$title = Inflector::humanize($this->viewPath);
|
|
|
|
}
|
2009-09-29 01:23:40 +00:00
|
|
|
}
|
2014-01-09 15:45:49 +00:00
|
|
|
$this->viewVars['title_for_layout'] = $title;
|
|
|
|
$this->Blocks->set('title', $title);
|
2011-10-08 19:14:17 +00:00
|
|
|
|
2015-07-21 08:22:53 +00:00
|
|
|
$this->_currentType = static::TYPE_LAYOUT;
|
2011-12-18 03:53:21 +00:00
|
|
|
$this->Blocks->set('content', $this->_render($layoutFileName));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-12-25 23:30:19 +00:00
|
|
|
$this->getEventManager()->dispatch(new CakeEvent('View.afterLayout', $this, array($layoutFileName)));
|
2011-12-18 03:53:21 +00:00
|
|
|
return $this->Blocks->get('content');
|
2008-11-24 05:36:06 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-02-12 20:54:39 +00:00
|
|
|
* Render cached view. Works in concert with CacheHelper and Dispatcher to
|
2010-01-25 23:14:16 +00:00
|
|
|
* render cached view files.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $filename the cache file to include
|
|
|
|
* @param string $timeStart the page render start time
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success of rendering the cached file.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function renderCache($filename, $timeStart) {
|
2013-06-06 13:33:51 +00:00
|
|
|
$response = $this->response;
|
2008-05-30 11:40:08 +00:00
|
|
|
ob_start();
|
2013-10-15 23:40:45 +00:00
|
|
|
include $filename;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2013-06-06 13:33:51 +00:00
|
|
|
$type = $response->mapType($response->type());
|
|
|
|
if (Configure::read('debug') > 0 && $type === 'html') {
|
2016-12-05 21:14:33 +00:00
|
|
|
echo "<!-- Cached Render Time: " . round(microtime(true) - (int)$timeStart, 4) . "s -->";
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$out = ob_get_clean();
|
|
|
|
|
|
|
|
if (preg_match('/^<!--cachetime:(\\d+)-->/', $out, $match)) {
|
|
|
|
if (time() >= $match['1']) {
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreStart
|
2008-05-30 11:40:08 +00:00
|
|
|
@unlink($filename);
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreEnd
|
2013-03-19 01:17:24 +00:00
|
|
|
unset($out);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-02 23:14:41 +00:00
|
|
|
return substr($out, strlen($match[0]));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of variables available in the current View context
|
|
|
|
*
|
2010-01-25 23:14:16 +00:00
|
|
|
* @return array Array of the set view variable names.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function getVars() {
|
2008-05-30 11:40:08 +00:00
|
|
|
return array_keys($this->viewVars);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the contents of the given View variable(s)
|
|
|
|
*
|
2010-01-25 23:14:16 +00:00
|
|
|
* @param string $var The view var you want the contents of.
|
|
|
|
* @return mixed The content of the named var if its set, otherwise null.
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Will be removed in 3.0. Use View::get() instead.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function getVar($var) {
|
2011-08-17 01:10:23 +00:00
|
|
|
return $this->get($var);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-14 18:06:37 +00:00
|
|
|
* Returns the contents of the given View variable.
|
2011-08-17 01:10:23 +00:00
|
|
|
*
|
|
|
|
* @param string $var The view var you want the contents of.
|
2013-09-11 03:10:27 +00:00
|
|
|
* @param mixed $default The default/fallback content of $var.
|
2013-09-11 14:28:09 +00:00
|
|
|
* @return mixed The content of the named var if its set, otherwise $default.
|
2011-08-17 01:10:23 +00:00
|
|
|
*/
|
2013-09-11 03:10:27 +00:00
|
|
|
public function get($var, $default = null) {
|
2011-09-15 02:32:16 +00:00
|
|
|
if (!isset($this->viewVars[$var])) {
|
2013-09-11 03:10:27 +00:00
|
|
|
return $default;
|
2011-08-17 01:10:23 +00:00
|
|
|
}
|
|
|
|
return $this->viewVars[$var];
|
|
|
|
}
|
|
|
|
|
2011-08-17 01:23:09 +00:00
|
|
|
/**
|
|
|
|
* Get the names of all the existing blocks.
|
|
|
|
*
|
|
|
|
* @return array An array containing the blocks.
|
2011-09-30 02:11:15 +00:00
|
|
|
* @see ViewBlock::keys()
|
2011-08-17 01:23:09 +00:00
|
|
|
*/
|
|
|
|
public function blocks() {
|
2011-09-30 02:11:15 +00:00
|
|
|
return $this->Blocks->keys();
|
2011-08-17 01:23:09 +00:00
|
|
|
}
|
|
|
|
|
2011-08-17 01:10:23 +00:00
|
|
|
/**
|
|
|
|
* Start capturing output for a 'block'
|
|
|
|
*
|
|
|
|
* @param string $name The name of the block to capture for.
|
|
|
|
* @return void
|
2011-09-30 02:11:15 +00:00
|
|
|
* @see ViewBlock::start()
|
2011-08-17 01:10:23 +00:00
|
|
|
*/
|
|
|
|
public function start($name) {
|
2013-07-29 08:05:55 +00:00
|
|
|
$this->Blocks->start($name);
|
2011-08-17 01:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-10-28 22:33:19 +00:00
|
|
|
* Start capturing output for a 'block' if it has no content
|
|
|
|
*
|
|
|
|
* @param string $name The name of the block to capture for.
|
|
|
|
* @return void
|
|
|
|
* @see ViewBlock::startIfEmpty()
|
|
|
|
*/
|
|
|
|
public function startIfEmpty($name) {
|
2013-07-29 08:05:55 +00:00
|
|
|
$this->Blocks->startIfEmpty($name);
|
2012-10-28 22:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Append to an existing or new block. Appending to a new
|
2011-08-17 01:10:23 +00:00
|
|
|
* block will create the block.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the block
|
2013-07-27 04:51:30 +00:00
|
|
|
* @param mixed $value The content for the block.
|
2011-08-17 01:10:23 +00:00
|
|
|
* @return void
|
2012-09-30 12:48:32 +00:00
|
|
|
* @see ViewBlock::concat()
|
2011-08-17 01:10:23 +00:00
|
|
|
*/
|
|
|
|
public function append($name, $value = null) {
|
2013-07-29 08:05:55 +00:00
|
|
|
$this->Blocks->concat($name, $value);
|
2012-09-30 12:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepend to an existing or new block. Prepending to a new
|
|
|
|
* block will create the block.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the block
|
2013-07-27 04:51:30 +00:00
|
|
|
* @param mixed $value The content for the block.
|
2012-09-30 12:48:32 +00:00
|
|
|
* @return void
|
|
|
|
* @see ViewBlock::concat()
|
|
|
|
*/
|
|
|
|
public function prepend($name, $value = null) {
|
2013-07-29 08:05:55 +00:00
|
|
|
$this->Blocks->concat($name, $value, ViewBlock::PREPEND);
|
2011-08-17 01:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Set the content for a block. This will overwrite any
|
2011-08-17 01:10:23 +00:00
|
|
|
* existing content.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the block
|
2013-07-27 04:51:30 +00:00
|
|
|
* @param mixed $value The content for the block.
|
2011-08-17 01:10:23 +00:00
|
|
|
* @return void
|
2012-08-30 04:53:29 +00:00
|
|
|
* @see ViewBlock::set()
|
2011-08-17 01:10:23 +00:00
|
|
|
*/
|
2011-09-22 02:32:39 +00:00
|
|
|
public function assign($name, $value) {
|
2013-07-29 08:05:55 +00:00
|
|
|
$this->Blocks->set($name, $value);
|
2011-08-17 01:10:23 +00:00
|
|
|
}
|
|
|
|
|
2011-09-15 02:32:16 +00:00
|
|
|
/**
|
2011-12-18 02:41:06 +00:00
|
|
|
* Fetch the content for a block. If a block is
|
2012-04-16 23:30:10 +00:00
|
|
|
* empty or undefined '' will be returned.
|
2011-09-15 02:32:16 +00:00
|
|
|
*
|
|
|
|
* @param string $name Name of the block
|
2012-11-28 22:30:47 +00:00
|
|
|
* @param string $default Default text
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return string default The block content or $default if the block does not exist.
|
2012-08-30 04:53:29 +00:00
|
|
|
* @see ViewBlock::get()
|
2011-09-15 02:32:16 +00:00
|
|
|
*/
|
2012-09-13 19:42:15 +00:00
|
|
|
public function fetch($name, $default = '') {
|
|
|
|
return $this->Blocks->get($name, $default);
|
2011-09-15 02:32:16 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 14:44:11 +00:00
|
|
|
/**
|
|
|
|
* Check if a block exists
|
|
|
|
*
|
|
|
|
* @param string $name Name of the block
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function exists($name) {
|
|
|
|
return $this->Blocks->exists($name);
|
|
|
|
}
|
|
|
|
|
2011-08-17 01:10:23 +00:00
|
|
|
/**
|
|
|
|
* End a capturing block. The compliment to View::start()
|
|
|
|
*
|
|
|
|
* @return void
|
2012-08-30 04:53:29 +00:00
|
|
|
* @see ViewBlock::end()
|
2011-08-17 01:10:23 +00:00
|
|
|
*/
|
|
|
|
public function end() {
|
2013-07-29 08:05:55 +00:00
|
|
|
$this->Blocks->end();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-09-23 01:07:21 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Provides view or element extension/inheritance. Views can extends a
|
2011-09-23 01:07:21 +00:00
|
|
|
* parent view and populate blocks in the parent template.
|
|
|
|
*
|
|
|
|
* @param string $name The view or element to 'extend' the current one with.
|
2011-09-30 02:11:15 +00:00
|
|
|
* @return void
|
2011-12-24 05:03:51 +00:00
|
|
|
* @throws LogicException when you extend a view with itself or make extend loops.
|
2012-01-25 11:43:55 +00:00
|
|
|
* @throws LogicException when you extend an element which doesn't exist
|
2011-09-23 01:07:21 +00:00
|
|
|
*/
|
|
|
|
public function extend($name) {
|
2015-07-21 08:22:53 +00:00
|
|
|
if ($name[0] === '/' || $this->_currentType === static::TYPE_VIEW) {
|
2012-01-25 11:43:55 +00:00
|
|
|
$parent = $this->_getViewFileName($name);
|
|
|
|
} else {
|
|
|
|
switch ($this->_currentType) {
|
2015-07-21 08:22:53 +00:00
|
|
|
case static::TYPE_ELEMENT:
|
2012-01-25 11:43:55 +00:00
|
|
|
$parent = $this->_getElementFileName($name);
|
|
|
|
if (!$parent) {
|
2012-02-08 04:14:56 +00:00
|
|
|
list($plugin, $name) = $this->pluginSplit($name);
|
2012-01-25 11:43:55 +00:00
|
|
|
$paths = $this->_paths($plugin);
|
|
|
|
$defaultPath = $paths[0] . 'Elements' . DS;
|
|
|
|
throw new LogicException(__d(
|
|
|
|
'cake_dev',
|
|
|
|
'You cannot extend an element which does not exist (%s).',
|
|
|
|
$defaultPath . $name . $this->ext
|
|
|
|
));
|
|
|
|
}
|
|
|
|
break;
|
2015-07-21 08:22:53 +00:00
|
|
|
case static::TYPE_LAYOUT:
|
2012-01-25 11:43:55 +00:00
|
|
|
$parent = $this->_getLayoutFileName($name);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$parent = $this->_getViewFileName($name);
|
|
|
|
}
|
2011-09-26 01:52:11 +00:00
|
|
|
}
|
2012-01-24 12:54:39 +00:00
|
|
|
|
2011-12-24 04:56:48 +00:00
|
|
|
if ($parent == $this->_current) {
|
|
|
|
throw new LogicException(__d('cake_dev', 'You cannot have views extend themselves.'));
|
|
|
|
}
|
2011-12-24 05:03:51 +00:00
|
|
|
if (isset($this->_parents[$parent]) && $this->_parents[$parent] == $this->_current) {
|
|
|
|
throw new LogicException(__d('cake_dev', 'You cannot have views extend in a loop.'));
|
|
|
|
}
|
2011-09-26 01:52:11 +00:00
|
|
|
$this->_parents[$this->_current] = $parent;
|
2011-09-23 01:07:21 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Adds a script block or other element to be inserted in $scripts_for_layout in
|
2010-01-25 23:14:16 +00:00
|
|
|
* the `<head />` of a document layout
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 23:14:16 +00:00
|
|
|
* @param string $name Either the key name for the script, or the script content. Name can be used to
|
|
|
|
* update/replace a script element.
|
|
|
|
* @param string $content The content of the script being added, optional.
|
2008-08-03 05:24:10 +00:00
|
|
|
* @return void
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Will be removed in 3.0. Superseded by blocks functionality.
|
2011-08-17 01:23:09 +00:00
|
|
|
* @see View::start()
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function addScript($name, $content = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($content)) {
|
2010-04-05 11:15:17 +00:00
|
|
|
if (!in_array($name, array_values($this->_scripts))) {
|
|
|
|
$this->_scripts[] = $name;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-04-05 11:15:17 +00:00
|
|
|
$this->_scripts[$name] = $content;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generates a unique, non-random DOM ID for an object, based on the object type and the target URL.
|
|
|
|
*
|
|
|
|
* @param string $object Type of object, i.e. 'form' or 'link'
|
|
|
|
* @param string $url The object's target URL
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function uuid($object, $url) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$c = 1;
|
|
|
|
$url = Router::url($url);
|
|
|
|
$hash = $object . substr(md5($object . $url), 0, 10);
|
|
|
|
while (in_array($hash, $this->uuids)) {
|
|
|
|
$hash = $object . substr(md5($object . $url . $c), 0, 10);
|
|
|
|
$c++;
|
|
|
|
}
|
|
|
|
$this->uuids[] = $hash;
|
|
|
|
return $hash;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Allows a template or element to set a variable that will be available in
|
2011-12-02 05:58:09 +00:00
|
|
|
* a layout or other element. Analogous to Controller::set().
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $one A string or an array of data.
|
2018-12-22 20:09:44 +00:00
|
|
|
* @param mixed $two Value in case $one is a string (which then works as the key).
|
2009-09-29 01:23:40 +00:00
|
|
|
* Unused if $one is an associative array, otherwise serves as the values to $one's keys.
|
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function set($one, $two = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$data = null;
|
|
|
|
if (is_array($one)) {
|
|
|
|
if (is_array($two)) {
|
|
|
|
$data = array_combine($one, $two);
|
|
|
|
} else {
|
|
|
|
$data = $one;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data = array($one => $two);
|
|
|
|
}
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$data) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-09-18 04:13:03 +00:00
|
|
|
$this->viewVars = $data + $this->viewVars;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2014-06-27 15:21:07 +00:00
|
|
|
/**
|
2014-06-27 18:03:29 +00:00
|
|
|
* Retrieve the current view type
|
2014-06-27 15:21:07 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-06-27 18:03:29 +00:00
|
|
|
public function getCurrentType() {
|
2014-06-27 15:21:07 +00:00
|
|
|
return $this->_currentType;
|
|
|
|
}
|
2010-07-03 03:50:41 +00:00
|
|
|
/**
|
2010-09-11 03:55:42 +00:00
|
|
|
* Magic accessor for helpers. Provides access to attributes that were deprecated.
|
2010-07-03 03:50:41 +00:00
|
|
|
*
|
2010-09-11 03:55:42 +00:00
|
|
|
* @param string $name Name of the attribute to get.
|
|
|
|
* @return mixed
|
2010-07-03 03:50:41 +00:00
|
|
|
*/
|
|
|
|
public function __get($name) {
|
2010-09-11 03:38:15 +00:00
|
|
|
switch ($name) {
|
2010-09-11 03:55:42 +00:00
|
|
|
case 'base':
|
|
|
|
case 'here':
|
|
|
|
case 'webroot':
|
|
|
|
case 'data':
|
|
|
|
return $this->request->{$name};
|
2010-09-11 03:38:15 +00:00
|
|
|
case 'action':
|
2012-01-12 10:48:51 +00:00
|
|
|
return $this->request->params['action'];
|
2010-09-11 03:55:42 +00:00
|
|
|
case 'params':
|
|
|
|
return $this->request;
|
2011-10-08 19:14:17 +00:00
|
|
|
case 'output':
|
|
|
|
return $this->Blocks->get('content');
|
2010-09-11 03:38:15 +00:00
|
|
|
}
|
2012-03-28 04:14:23 +00:00
|
|
|
if (isset($this->Helpers->{$name})) {
|
|
|
|
$this->{$name} = $this->Helpers->{$name};
|
|
|
|
return $this->Helpers->{$name};
|
|
|
|
}
|
|
|
|
return $this->{$name};
|
2010-07-03 03:50:41 +00:00
|
|
|
}
|
|
|
|
|
2011-10-08 19:14:17 +00:00
|
|
|
/**
|
|
|
|
* Magic accessor for deprecated attributes.
|
2011-12-29 15:22:08 +00:00
|
|
|
*
|
2011-10-08 19:14:17 +00:00
|
|
|
* @param string $name Name of the attribute to set.
|
2013-07-27 04:51:30 +00:00
|
|
|
* @param mixed $value Value of the attribute to set.
|
2011-10-08 19:14:17 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __set($name, $value) {
|
|
|
|
switch ($name) {
|
|
|
|
case 'output':
|
|
|
|
return $this->Blocks->set('content', $value);
|
2011-12-18 03:53:21 +00:00
|
|
|
default:
|
|
|
|
$this->{$name} = $value;
|
2011-10-08 19:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-29 15:22:08 +00:00
|
|
|
/**
|
|
|
|
* Magic isset check for deprecated attributes.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the attribute to check.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2011-12-29 15:22:08 +00:00
|
|
|
*/
|
|
|
|
public function __isset($name) {
|
2012-01-12 10:48:51 +00:00
|
|
|
if (isset($this->{$name})) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$magicGet = array('base', 'here', 'webroot', 'data', 'action', 'params', 'output');
|
|
|
|
if (in_array($name, $magicGet)) {
|
|
|
|
return $this->__get($name) !== null;
|
|
|
|
}
|
|
|
|
return false;
|
2011-12-29 15:22:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-03 03:50:41 +00:00
|
|
|
/**
|
|
|
|
* Interact with the HelperCollection to load all the helpers.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function loadHelpers() {
|
2010-07-04 20:57:27 +00:00
|
|
|
$helpers = HelperCollection::normalizeObjectArray($this->helpers);
|
2012-09-05 22:22:30 +00:00
|
|
|
foreach ($helpers as $properties) {
|
2012-12-20 12:47:03 +00:00
|
|
|
list(, $class) = pluginSplit($properties['class']);
|
2011-01-23 22:51:56 +00:00
|
|
|
$this->{$class} = $this->Helpers->load($properties['class'], $properties['settings']);
|
2010-07-03 03:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Renders and returns output for given view filename with its
|
2011-09-23 01:07:21 +00:00
|
|
|
* array of data. Handles parent/extended views.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-09-23 01:07:21 +00:00
|
|
|
* @param string $viewFile Filename of the view
|
|
|
|
* @param array $data Data to include in rendered view. If empty the current View::$viewVars will be used.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Rendered output
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers View.beforeRenderFile $this, array($viewFile)
|
|
|
|
* @triggers View.afterRenderFile $this, array($viewFile, $content)
|
2011-12-13 02:43:41 +00:00
|
|
|
* @throws CakeException when a block is left open.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-09-23 01:07:21 +00:00
|
|
|
protected function _render($viewFile, $data = array()) {
|
|
|
|
if (empty($data)) {
|
|
|
|
$data = $this->viewVars;
|
|
|
|
}
|
|
|
|
$this->_current = $viewFile;
|
2011-12-30 19:56:31 +00:00
|
|
|
$initialBlocks = count($this->Blocks->unclosed());
|
2011-10-08 18:47:25 +00:00
|
|
|
|
2012-11-10 03:59:55 +00:00
|
|
|
$eventManager = $this->getEventManager();
|
|
|
|
$beforeEvent = new CakeEvent('View.beforeRenderFile', $this, array($viewFile));
|
|
|
|
|
|
|
|
$eventManager->dispatch($beforeEvent);
|
2011-09-23 01:07:21 +00:00
|
|
|
$content = $this->_evaluate($viewFile, $data);
|
2012-11-10 03:59:55 +00:00
|
|
|
|
2011-12-25 23:30:19 +00:00
|
|
|
$afterEvent = new CakeEvent('View.afterRenderFile', $this, array($viewFile, $content));
|
2012-11-14 12:16:08 +00:00
|
|
|
|
2011-12-25 23:30:19 +00:00
|
|
|
$afterEvent->modParams = 1;
|
2012-11-10 03:59:55 +00:00
|
|
|
$eventManager->dispatch($afterEvent);
|
2011-12-25 23:30:19 +00:00
|
|
|
$content = $afterEvent->data[1];
|
2011-09-23 01:07:21 +00:00
|
|
|
|
|
|
|
if (isset($this->_parents[$viewFile])) {
|
|
|
|
$this->_stack[] = $this->fetch('content');
|
|
|
|
$this->assign('content', $content);
|
|
|
|
|
2011-12-30 19:56:31 +00:00
|
|
|
$content = $this->_render($this->_parents[$viewFile]);
|
2011-09-23 01:07:21 +00:00
|
|
|
$this->assign('content', array_pop($this->_stack));
|
2010-11-01 03:15:34 +00:00
|
|
|
}
|
2008-06-15 19:48:15 +00:00
|
|
|
|
2011-12-30 19:56:31 +00:00
|
|
|
$remainingBlocks = count($this->Blocks->unclosed());
|
|
|
|
|
|
|
|
if ($initialBlocks !== $remainingBlocks) {
|
|
|
|
throw new CakeException(__d('cake_dev', 'The "%s" block was left open. Blocks are not allowed to cross files.', $this->Blocks->active()));
|
|
|
|
}
|
|
|
|
|
2011-09-23 01:07:21 +00:00
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sandbox method to evaluate a template / view script in.
|
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param string $viewFile Filename of the view
|
2012-09-30 18:14:32 +00:00
|
|
|
* @param array $dataForView Data to include in rendered view.
|
2011-09-23 01:07:21 +00:00
|
|
|
* If empty the current View::$viewVars will be used.
|
|
|
|
* @return string Rendered output
|
|
|
|
*/
|
2012-05-20 20:06:28 +00:00
|
|
|
protected function _evaluate($viewFile, $dataForView) {
|
|
|
|
$this->__viewFile = $viewFile;
|
|
|
|
extract($dataForView);
|
2008-05-30 11:40:08 +00:00
|
|
|
ob_start();
|
|
|
|
|
2012-05-20 20:06:28 +00:00
|
|
|
include $this->__viewFile;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-08-25 06:11:25 +00:00
|
|
|
unset($this->__viewFile);
|
2010-11-06 04:07:51 +00:00
|
|
|
return ob_get_clean();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-02 02:47:20 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Loads a helper. Delegates to the `HelperCollection::load()` to load the helper
|
2010-07-02 02:47:20 +00:00
|
|
|
*
|
|
|
|
* @param string $helperName Name of the helper to load.
|
|
|
|
* @param array $settings Settings for the helper
|
|
|
|
* @return Helper a constructed helper object.
|
2010-11-07 03:48:27 +00:00
|
|
|
* @see HelperCollection::load()
|
2010-07-02 02:47:20 +00:00
|
|
|
*/
|
2010-11-07 03:48:27 +00:00
|
|
|
public function loadHelper($helperName, $settings = array()) {
|
|
|
|
return $this->Helpers->load($helperName, $settings);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns filename of given action's template file (.ctp) as a string.
|
|
|
|
* CamelCased action names will be under_scored! This means that you can have
|
|
|
|
* LongActionNames that refer to long_action_names.ctp views.
|
|
|
|
*
|
2010-01-25 23:14:16 +00:00
|
|
|
* @param string $name Controller action to find template filename for
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Template filename
|
2010-07-03 05:00:09 +00:00
|
|
|
* @throws MissingViewException when a view file could not be found.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _getViewFileName($name = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$subDir = null;
|
|
|
|
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($this->subDir !== null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$subDir = $this->subDir . DS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($name === null) {
|
2011-03-08 02:50:53 +00:00
|
|
|
$name = $this->view;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$name = str_replace('/', DS, $name);
|
2012-02-08 04:14:56 +00:00
|
|
|
list($plugin, $name) = $this->pluginSplit($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-06-08 15:44:46 +00:00
|
|
|
if (strpos($name, DS) === false && $name[0] !== '.') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$name = $this->viewPath . DS . $subDir . Inflector::underscore($name);
|
|
|
|
} elseif (strpos($name, DS) !== false) {
|
2011-03-09 05:35:58 +00:00
|
|
|
if ($name[0] === DS || $name[1] === ':') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$name = trim($name, DS);
|
2012-02-23 13:38:02 +00:00
|
|
|
} elseif ($name[0] === '.') {
|
2008-06-08 15:44:46 +00:00
|
|
|
$name = substr($name, 3);
|
2012-03-08 21:00:57 +00:00
|
|
|
} elseif (!$plugin || $this->viewPath !== $this->name) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$name = $this->viewPath . DS . $subDir . $name;
|
|
|
|
}
|
|
|
|
}
|
2011-11-13 02:48:26 +00:00
|
|
|
$paths = $this->_paths($plugin);
|
2011-01-13 01:43:41 +00:00
|
|
|
$exts = $this->_getExtensions();
|
2009-08-30 23:13:54 +00:00
|
|
|
foreach ($exts as $ext) {
|
|
|
|
foreach ($paths as $path) {
|
|
|
|
if (file_exists($path . $name . $ext)) {
|
|
|
|
return $path . $name . $ext;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-17 20:30:50 +00:00
|
|
|
throw new MissingViewException(array('file' => $name . $this->ext));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 03:31:34 +00:00
|
|
|
/**
|
|
|
|
* Splits a dot syntax plugin name into its plugin and filename.
|
|
|
|
* If $name does not have a dot, then index 0 will be null.
|
|
|
|
* It checks if the plugin is loaded, else filename will stay unchanged for filenames containing dot
|
|
|
|
*
|
|
|
|
* @param string $name The name you want to plugin split.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $fallback If true uses the plugin set in the current CakeRequest when parsed plugin is not loaded
|
2012-12-22 22:48:15 +00:00
|
|
|
* @return array Array with 2 indexes. 0 => plugin name, 1 => filename
|
2011-11-13 03:31:34 +00:00
|
|
|
*/
|
2012-02-08 04:14:56 +00:00
|
|
|
public function pluginSplit($name, $fallback = true) {
|
2011-11-13 02:48:26 +00:00
|
|
|
$plugin = null;
|
|
|
|
list($first, $second) = pluginSplit($name);
|
|
|
|
if (CakePlugin::loaded($first) === true) {
|
|
|
|
$name = $second;
|
|
|
|
$plugin = $first;
|
|
|
|
}
|
2012-02-08 04:14:56 +00:00
|
|
|
if (isset($this->plugin) && !$plugin && $fallback) {
|
2011-11-13 02:48:26 +00:00
|
|
|
$plugin = $this->plugin;
|
|
|
|
}
|
|
|
|
return array($plugin, $name);
|
|
|
|
}
|
2011-11-13 03:31:34 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns layout filename for this template as a string.
|
|
|
|
*
|
2010-01-25 23:14:16 +00:00
|
|
|
* @param string $name The name of the layout to find.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Filename for layout file (.ctp).
|
2010-07-03 05:00:09 +00:00
|
|
|
* @throws MissingLayoutException when a layout cannot be located
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _getLayoutFileName($name = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($name === null) {
|
|
|
|
$name = $this->layout;
|
|
|
|
}
|
|
|
|
$subDir = null;
|
|
|
|
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($this->layoutPath !== null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$subDir = $this->layoutPath . DS;
|
|
|
|
}
|
2012-02-08 04:14:56 +00:00
|
|
|
list($plugin, $name) = $this->pluginSplit($name);
|
2011-11-13 02:48:26 +00:00
|
|
|
$paths = $this->_paths($plugin);
|
2011-05-13 07:23:35 +00:00
|
|
|
$file = 'Layouts' . DS . $subDir . $name;
|
2011-01-13 01:43:41 +00:00
|
|
|
|
|
|
|
$exts = $this->_getExtensions();
|
2009-08-30 23:13:54 +00:00
|
|
|
foreach ($exts as $ext) {
|
|
|
|
foreach ($paths as $path) {
|
|
|
|
if (file_exists($path . $file . $ext)) {
|
|
|
|
return $path . $file . $ext;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-17 20:30:50 +00:00
|
|
|
throw new MissingLayoutException(array('file' => $file . $this->ext));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-01-13 01:43:41 +00:00
|
|
|
/**
|
|
|
|
* Get the extensions that view files can use.
|
|
|
|
*
|
|
|
|
* @return array Array of extensions view files use.
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
protected function _getExtensions() {
|
2011-01-13 01:43:41 +00:00
|
|
|
$exts = array($this->ext);
|
|
|
|
if ($this->ext !== '.ctp') {
|
2013-01-02 22:47:27 +00:00
|
|
|
$exts[] = '.ctp';
|
2011-01-13 01:43:41 +00:00
|
|
|
}
|
|
|
|
return $exts;
|
|
|
|
}
|
|
|
|
|
2010-11-10 03:16:45 +00:00
|
|
|
/**
|
|
|
|
* Finds an element filename, returns false on failure.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the element to find.
|
|
|
|
* @return mixed Either a string to the element filename or false when one can't be found.
|
|
|
|
*/
|
2011-12-22 02:57:18 +00:00
|
|
|
protected function _getElementFileName($name) {
|
2012-02-08 04:14:56 +00:00
|
|
|
list($plugin, $name) = $this->pluginSplit($name);
|
2011-12-22 02:57:18 +00:00
|
|
|
|
2010-11-10 03:16:45 +00:00
|
|
|
$paths = $this->_paths($plugin);
|
2011-01-19 01:04:30 +00:00
|
|
|
$exts = $this->_getExtensions();
|
|
|
|
foreach ($exts as $ext) {
|
|
|
|
foreach ($paths as $path) {
|
2011-05-13 07:23:35 +00:00
|
|
|
if (file_exists($path . 'Elements' . DS . $name . $ext)) {
|
|
|
|
return $path . 'Elements' . DS . $name . $ext;
|
2011-01-19 01:04:30 +00:00
|
|
|
}
|
2010-11-10 03:16:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Return all possible paths to find view files in order
|
|
|
|
*
|
2010-01-25 23:14:16 +00:00
|
|
|
* @param string $plugin Optional plugin name to scan for view files.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $cached Set to false to force a refresh of view paths. Default true.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array paths
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _paths($plugin = null, $cached = true) {
|
2014-04-19 20:21:30 +00:00
|
|
|
if ($cached === true) {
|
|
|
|
if ($plugin === null && !empty($this->_paths)) {
|
|
|
|
return $this->_paths;
|
|
|
|
}
|
|
|
|
if ($plugin !== null && isset($this->_pathsForPlugin[$plugin])) {
|
|
|
|
return $this->_pathsForPlugin[$plugin];
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$paths = array();
|
2010-12-05 15:24:42 +00:00
|
|
|
$viewPaths = App::path('View');
|
2012-02-20 20:47:59 +00:00
|
|
|
$corePaths = array_merge(App::core('View'), App::core('Console/Templates/skel/View'));
|
2012-02-20 20:09:03 +00:00
|
|
|
|
2009-07-21 01:33:12 +00:00
|
|
|
if (!empty($plugin)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$count = count($viewPaths);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2012-02-20 20:47:59 +00:00
|
|
|
if (!in_array($viewPaths[$i], $corePaths)) {
|
2011-10-13 13:22:10 +00:00
|
|
|
$paths[] = $viewPaths[$i] . 'Plugin' . DS . $plugin . DS;
|
2009-08-24 02:43:51 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-12-19 02:39:08 +00:00
|
|
|
$paths = array_merge($paths, App::path('View', $plugin));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-12-05 17:37:01 +00:00
|
|
|
|
2012-02-20 21:51:04 +00:00
|
|
|
$paths = array_unique(array_merge($paths, $viewPaths));
|
2012-01-22 20:45:26 +00:00
|
|
|
if (!empty($this->theme)) {
|
2013-01-04 02:06:46 +00:00
|
|
|
$theme = Inflector::camelize($this->theme);
|
2012-01-22 20:45:26 +00:00
|
|
|
$themePaths = array();
|
2012-02-20 21:51:04 +00:00
|
|
|
foreach ($paths as $path) {
|
|
|
|
if (strpos($path, DS . 'Plugin' . DS) === false) {
|
2012-03-03 20:59:36 +00:00
|
|
|
if ($plugin) {
|
2013-01-04 02:06:46 +00:00
|
|
|
$themePaths[] = $path . 'Themed' . DS . $theme . DS . 'Plugin' . DS . $plugin . DS;
|
2012-01-22 20:45:26 +00:00
|
|
|
}
|
2013-01-04 02:06:46 +00:00
|
|
|
$themePaths[] = $path . 'Themed' . DS . $theme . DS;
|
2012-03-03 20:59:36 +00:00
|
|
|
}
|
2012-01-22 20:45:26 +00:00
|
|
|
}
|
|
|
|
$paths = array_merge($themePaths, $paths);
|
|
|
|
}
|
2012-02-20 21:51:04 +00:00
|
|
|
$paths = array_merge($paths, $corePaths);
|
2011-12-24 05:13:43 +00:00
|
|
|
if ($plugin !== null) {
|
2014-04-19 20:21:30 +00:00
|
|
|
return $this->_pathsForPlugin[$plugin] = $paths;
|
2011-12-24 05:13:43 +00:00
|
|
|
}
|
|
|
|
return $this->_paths = $paths;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-03-03 20:59:36 +00:00
|
|
|
|
2012-07-04 09:48:10 +00:00
|
|
|
/**
|
|
|
|
* Checks if an element is cached and returns the cached data if present
|
|
|
|
*
|
|
|
|
* @param string $name Element name
|
2012-11-28 22:30:47 +00:00
|
|
|
* @param string $data Data
|
2012-07-04 09:48:10 +00:00
|
|
|
* @param array $options Element options
|
2012-11-28 22:30:47 +00:00
|
|
|
* @return string|null
|
2012-07-04 09:48:10 +00:00
|
|
|
*/
|
2012-07-06 00:13:34 +00:00
|
|
|
protected function _elementCache($name, $data, $options) {
|
|
|
|
$plugin = null;
|
|
|
|
list($plugin, $name) = $this->pluginSplit($name);
|
|
|
|
|
2012-07-04 09:48:10 +00:00
|
|
|
$underscored = null;
|
|
|
|
if ($plugin) {
|
|
|
|
$underscored = Inflector::underscore($plugin);
|
|
|
|
}
|
|
|
|
$keys = array_merge(array($underscored, $name), array_keys($options), array_keys($data));
|
|
|
|
$this->elementCacheSettings = array(
|
|
|
|
'config' => $this->elementCache,
|
|
|
|
'key' => implode('_', $keys)
|
|
|
|
);
|
|
|
|
if (is_array($options['cache'])) {
|
|
|
|
$defaults = array(
|
|
|
|
'config' => $this->elementCache,
|
|
|
|
'key' => $this->elementCacheSettings['key']
|
|
|
|
);
|
|
|
|
$this->elementCacheSettings = array_merge($defaults, $options['cache']);
|
|
|
|
}
|
2012-07-05 22:22:39 +00:00
|
|
|
$this->elementCacheSettings['key'] = 'element_' . $this->elementCacheSettings['key'];
|
|
|
|
return Cache::read($this->elementCacheSettings['key'], $this->elementCacheSettings['config']);
|
2012-07-04 09:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders an element and fires the before and afterRender callbacks for it
|
|
|
|
* and writes to the cache if a cache is used
|
|
|
|
*
|
|
|
|
* @param string $file Element file path
|
|
|
|
* @param array $data Data to render
|
|
|
|
* @param array $options Element options
|
2012-11-28 22:30:47 +00:00
|
|
|
* @return string
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers View.beforeRender $this, array($file)
|
|
|
|
* @triggers View.afterRender $this, array($file, $element)
|
2012-07-04 09:48:10 +00:00
|
|
|
*/
|
|
|
|
protected function _renderElement($file, $data, $options) {
|
2012-10-02 01:20:25 +00:00
|
|
|
$current = $this->_current;
|
|
|
|
$restore = $this->_currentType;
|
2015-07-21 08:22:53 +00:00
|
|
|
$this->_currentType = static::TYPE_ELEMENT;
|
2014-06-26 03:08:59 +00:00
|
|
|
|
2012-07-05 22:22:39 +00:00
|
|
|
if ($options['callbacks']) {
|
2012-07-04 09:48:10 +00:00
|
|
|
$this->getEventManager()->dispatch(new CakeEvent('View.beforeRender', $this, array($file)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$element = $this->_render($file, array_merge($this->viewVars, $data));
|
|
|
|
|
2012-10-24 23:16:16 +00:00
|
|
|
if ($options['callbacks']) {
|
2012-07-04 09:48:10 +00:00
|
|
|
$this->getEventManager()->dispatch(new CakeEvent('View.afterRender', $this, array($file, $element)));
|
|
|
|
}
|
2014-06-26 03:08:59 +00:00
|
|
|
|
2012-10-02 01:20:25 +00:00
|
|
|
$this->_currentType = $restore;
|
|
|
|
$this->_current = $current;
|
|
|
|
|
2012-07-04 09:48:10 +00:00
|
|
|
if (isset($options['cache'])) {
|
2012-07-05 22:22:39 +00:00
|
|
|
Cache::write($this->elementCacheSettings['key'], $element, $this->elementCacheSettings['config']);
|
2012-07-04 09:48:10 +00:00
|
|
|
}
|
|
|
|
return $element;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|