View Class Info:
- Class Declaration:
class View extends Object
- File name:
- Cake/View/View.php
- Description:
View, the V in the MVC triad. View interacts with Helpers and view variables passed in from the controller to render the results of the controller action. Often this is HTML, but can also take the form of JSON, XML, PDF's or streaming files.
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 layout using
$this->set()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'in your Controller to use the Themes.Example of theme path with
$this->theme = 'SuperHot';Would beapp/View/Themed/SuperHot/Posts- Class Inheritance
- Package
- Cake.View
- Property
CacheHelper $Cache
FormHelper $Form
HtmlHelper $Html
JsHelper $Js
NumberHelper $Number
PaginatorHelper $Paginator
RssHelper $Rss
SessionHelper $Session
TextHelper $Text
TimeHelper $Time
ViewBlock $Blocks
Properties:
-
autoLayout boolean
Turns on or off Cake's conventional mode of applying layout files. On by default. Setting to off means that layouts will not be automatically applied to rendered views.
-
Blocks ViewBlock
ViewBlock instance.
-
cacheAction mixed
Used to define methods a controller that will be cached.
-
_current string
The currently rendering view file. Used for resolving parent files.
-
_currentType string
Currently rendering an element. Used for finding parent fragments for elements.
-
elementCache string
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 per element.
-
_eventManager CakeEventManager
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.
-
_eventManagerConfigured boolean
Whether the event manager was already configured for this object
-
ext string
File extension. Defaults to Cake's template ".ctp".
-
hasRendered boolean
True when the view has been rendered.
-
helpers mixed A single name as a string or a list of names as an array.
An array of names of built-in helpers to include.
-
Helpers HelperCollection
Helpers collection
-
_helpersLoaded boolean
Indicate that helpers have been loaded.
-
layout string
Name of layout to use with this View.
-
layoutPath string Path to Layout
Path to Layout.
-
name string Name of controller
Name of the controller.
-
_parents array
The names of views and their parents used with View::extend();
-
passedArgs mixed
Current passed params
-
_passedVars array
List of variables to collect from the associated controller.
-
_paths array
Holds an array of paths.
-
plugin string
Name of the plugin.
-
request CakeRequest
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 additional information about the request.
-
response CakeResponse
Reference to the Response object
-
_scripts array
Scripts (and/or other <head /> tags) for the layout.
-
_stack array
Content stack, used for nested templates that all use View::extend();
-
subDir string
Sub-directory for this view file. This is often used for extension based routing. Eg. With an
xmlextension, $subDir would bexml/ -
theme string
Theme name.
-
uuids array
List of generated DOM UUIDs.
-
validationErrors array
Holds current errors for the model validation.
-
view string
Name of view to use with this View.
-
viewPath string Path to View
Path to View.
-
viewVars array
Variables for the view
Method Summary:
- addScript( $name, $content = NULL )
- append( $name, $value = NULL )
- assign( $name, $value )
- blocks( )
- __construct( $controller = NULL )
- dispatchMethod( $method, $params = array ( ) )
- element( $name, $data = array ( ), $options = array ( ) )
- end( )
- _evaluate( $___viewFn, $___dataForView )
- extend( $name )
- fetch( $name )
- __get( $name )
- get( $var )
- _getElementFileName( $name )
- getEventManager( )
- _getExtensions( )
- _getLayoutFileName( $name = NULL )
- getVar( $var )
- getVars( )
- _getViewFileName( $name = NULL )
- __isset( $name )
- loadHelper( $helperName, $settings = array ( ) )
- loadHelpers( )
- log( $msg, $type = 2 )
- _mergeVars( $properties, $class, $normalize = true )
- _paths( $plugin = NULL, $cached = true )
- pluginSplit( $name, $fallback = true )
- render( $view = NULL, $layout = NULL )
- _render( $viewFile, $data = array ( ) )
- renderCache( $filename, $timeStart )
- renderLayout( $content, $layout = NULL )
- requestAction( $url, $extra = array ( ) )
- _set( $properties = array ( ) )
- set( $one, $two = NULL )
- __set( $name, $value )
- start( $name )
- _stop( $status = 0 )
- toString( )
- uuid( $object, $url )
addScript
topAdds a script block or other element to be inserted in $scripts_for_layout in
the <head /> of a document layout
- Parameters:
-
-
string $name required
Either the key name for the script, or the script content. Name can be used to update/replace a script element.
-
string $content optional NULL
The content of the script being added, optional.
-
- Method defined in:
- Cake/View/View.php on line 726
- Return
void
- Deprecated
Will be removed in 3.0. Supersceeded by blocks functionality.
- See
append
topAppend to an existing or new block. Appending to a new block will create the block.
- Parameters:
-
-
string $name required
Name of the block
-
string $value optional NULL
The content for the block.
-
- Method defined in:
- Cake/View/View.php on line 631
- Return
void
- Throws
CakeException when you use non-string values.
- See
assign
topSet the content for a block. This will overwrite any existing content.
- Parameters:
-
-
string $name required
Name of the block
-
string $value required
The content for the block.
-
- Method defined in:
- Cake/View/View.php on line 645
- Return
void
- Throws
CakeException when you use non-string values.
- See
blocks
topGet the names of all the existing blocks.
- Method defined in:
- Cake/View/View.php on line 606
- Return
array An array containing the blocks.
- See
__construct
topConstructor
- Parameters:
-
-
Controller $controller optional NULL
A controller object to pull View::_passedVars from.
-
- Method defined in:
- Cake/View/View.php on line 308
dispatchMethod
topCalls a method on this object with the given parameters. Provides an OO wrapper
for call_user_func_array
- Parameters:
-
-
string $method required
Name of the method to call
-
array $params optional array ( )
Parameter list to use when calling $method
-
- Method defined in:
- Cake/Core/Object.php on line 110
- Return
mixed Returns the result of the method call
element
topRenders 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 improving performance by using the cache option.
cache- Can either betrue, to enable caching using the config in View::$elementCache. Or an array If an array, the following keys can be used:- <code>config</code> - Used to store the cached element in a custom cache configuration.
- <code>key</code> - Used to define the key used in the <a href="/class/cache#method-Cachewrite">Cache::write()</a>. It will be prefixed with <code>element_</code>
plugin- Load an element from a specific plugin. This option is deprecated, see below.callbacks- Set to true to fire beforeRender and afterRender helper callbacks for this element. Defaults to false.
- Parameters:
-
-
string $name required
Name of template file in the/app/View/Elements/ folder, or
MyPlugin.templateto use the template element from MyPlugin. If the element is not found in the plugin, the normal view path cascade will be searched. -
array $data optional array ( )
Array of data to be made available to the rendered view (i.e. the Element)
-
array $options optional array ( )
Array of options. Possible keys are:
-
- Method defined in:
- Cake/View/View.php on line 365
- Return
string Rendered Element
- Deprecated
The
$options['plugin']is deprecated and will be removed in CakePHP 3.0. UsePlugin.element_nameinstead.
end
topEnd a capturing block. The compliment to View::start()
- Method defined in:
- Cake/View/View.php on line 667
- Return
void
- See
_evaluate
topSandbox method to evaluate a template / view script in.
- Parameters:
-
-
string $___viewFn required
Filename of the view
-
array $___dataForView required
Data to include in rendered view. If empty the current View::$viewVars will be used.
-
- Method defined in:
- Cake/View/View.php on line 904
- Return
string Rendered output
extend
topProvides view or element extension/inheritance. Views can extends a parent view and populate blocks in the parent template.
- Parameters:
-
-
string $name required
The view or element to 'extend' the current one with.
-
- Method defined in:
- Cake/View/View.php on line 680
- Return
void
- Throws
LogicException when you extend a view with itself or make extend loops.
LogicException when you extend an element which doesn't exist
fetch
topFetch the content for a block. If a block is empty or undefined '' will be returned.
- Parameters:
-
-
string $name required
Name of the block
-
- Method defined in:
- Cake/View/View.php on line 657
- Return
The block content or '' if the block does not exist.
- See
__get
topMagic accessor for helpers. Provides access to attributes that were deprecated.
- Parameters:
-
-
string $name required
Name of the attribute to get.
-
- Method defined in:
- Cake/View/View.php on line 787
- Return
mixed
get
topReturns the contents of the given View variable or a block. Blocks are checked before view variables.
- Parameters:
-
-
string $var required
The view var you want the contents of.
-
- Method defined in:
- Cake/View/View.php on line 593
- Return
mixed The content of the named var if its set, otherwise null.
_getElementFileName
topFinds an element filename, returns false on failure.
- Parameters:
-
-
string $name required
The name of the element to find.
-
- Method defined in:
- Cake/View/View.php on line 1056
- Return
mixed Either a string to the element filename or false when one can't be found.
getEventManager
topReturns 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.
- Method defined in:
- Cake/View/View.php on line 334
- Return
CakeEventManager
_getExtensions
topGet the extensions that view files can use.
- Method defined in:
- Cake/View/View.php on line 1042
- Return
array Array of extensions view files use.
_getLayoutFileName
topReturns layout filename for this template as a string.
- Parameters:
-
-
string $name optional NULL
The name of the layout to find.
-
- Method defined in:
- Cake/View/View.php on line 1013
- Return
string Filename for layout file (.ctp).
- Throws
MissingLayoutException when a layout cannot be located
getVar
topReturns the contents of the given View variable(s)
- Parameters:
-
-
string $var required
The view var you want the contents of.
-
- Method defined in:
- Cake/View/View.php on line 582
- Return
mixed The content of the named var if its set, otherwise null.
- Deprecated
Will be removed in 3.0 Use View::get() instead.
getVars
topReturns a list of variables available in the current View context
- Method defined in:
- Cake/View/View.php on line 571
- Return
array Array of the set view variable names.
_getViewFileName
topReturns 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.
- Parameters:
-
-
string $name optional NULL
Controller action to find template filename for
-
- Method defined in:
- Cake/View/View.php on line 934
- Return
string Template filename
- Throws
MissingViewException when a view file could not be found.
__isset
topMagic isset check for deprecated attributes.
- Parameters:
-
-
string $name required
Name of the attribute to check.
-
- Method defined in:
- Cake/View/View.php on line 830
- Return
boolean
loadHelper
topLoads a helper. Delegates to the HelperCollection::load() to load the helper
- Parameters:
-
-
string $helperName required
Name of the helper to load.
-
array $settings optional array ( )
Settings for the helper
-
- Method defined in:
- Cake/View/View.php on line 921
- Return
Helper a constructed helper object.
- See
loadHelpers
topInteract with the HelperCollection to load all the helpers.
- Method defined in:
- Cake/View/View.php on line 846
- Return
void
log
topConvenience method to write a message to CakeLog. See CakeLog::write() for more information on writing to logs.
- Parameters:
-
-
string $msg required
Log message
-
integer $type optional 2
Error type constant. Defined in app/Config/core.php.
-
- Method defined in:
- Cake/Core/Object.php on line 149
- Return
boolean Success of log write
_mergeVars
topMerges this objects $property with the property in $class' definition. This classes value for the property will be merged on top of $class'
This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine this method as an empty function.
- Parameters:
-
-
array $properties required
The name of the properties to merge.
-
string $class required
The class to merge the property with.
-
boolean $normalize optional true
Set to true to run the properties through Set::normalize() before merging.
-
- Method defined in:
- Cake/Core/Object.php on line 187
- Return
void
_paths
topReturn all possible paths to find view files in order
- Parameters:
-
-
string $plugin optional NULL
Optional plugin name to scan for view files.
-
boolean $cached optional true
Set to true to force a refresh of view paths.
-
- Method defined in:
- Cake/View/View.php on line 1078
- Return
array paths
pluginSplit
topSplits 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
- Parameters:
-
-
string $name required
The name you want to plugin split.
-
boolean $fallback optional true
If true uses the plugin set in the current CakeRequest when parsed plugin is not loaded
-
- Method defined in:
- Cake/View/View.php on line 993
- Return
array Array with 2 indexes. 0 => plugin name, 1 => filename
render
topRenders view for given view file and layout.
Render triggers helper callbacks, which are fired before and after the view are rendered, as well as before and after the layout. The helper callbacks are called:
beforeRenderafterRenderbeforeLayoutafterLayout
If View::$autoRender is false and no $layout is provided, the view will be returned bare.
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
the view will be located along the regular view path cascade.
- Parameters:
-
-
string $view optional NULL
Name of view file to use
-
string $layout optional NULL
Layout to use.
-
- Method defined in:
- Cake/View/View.php on line 451
- Return
string Rendered Element
- Throws
CakeException if there is an error in the view.
_render
topRenders and returns output for given view filename with its array of data. Handles parent/extended views.
- Parameters:
-
-
string $viewFile required
Filename of the view
-
array $data optional array ( )
Data to include in rendered view. If empty the current View::$viewVars will be used.
-
- Method defined in:
- Cake/View/View.php on line 864
- Return
string Rendered output
- Throws
CakeException when a block is left open.
renderCache
topRender cached view. Works in concert with CacheHelper and Dispatcher to render cached view files.
- Parameters:
-
-
string $filename required
the cache file to include
-
string $timeStart required
the page render start time
-
- Method defined in:
- Cake/View/View.php on line 541
- Return
boolean Success of rendering the cached file.
renderLayout
topRenders 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 filescripts_for_layout- Contains content added with addScript() as well as any content in the 'meta', 'css', and 'script' blocks. They are appended in that order.
Deprecated features:
$scripts_for_layoutis deprecated and will be removed in CakePHP 3.0. Use the block features instead.meta,cssandscriptwill be populated by the matching methods on HtmlHelper.$title_for_layoutis deprecated and will be removed in CakePHP 3.0$content_for_layoutis deprecated and will be removed in CakePHP 3.0. Use thecontentblock instead.
- Parameters:
-
-
string $content required
Content to render in a view, wrapped by the surrounding layout.
-
string $layout optional NULL
Layout name
-
- Method defined in:
- Cake/View/View.php on line 500
- Return
mixed Rendered output, or false on error
- Throws
CakeException if there is an error in the view.
requestAction
topCalls a controller's method from any location. Can be used to connect controllers together or tie plugins into a main application. requestAction can be used to return rendered views or fetch the return value from controller actions.
Under the hood this method uses Router::reverse() to convert the $url parameter into a string URL. You should use URL formats that are compatible with Router::reverse()
Passing POST and GET data
POST and GET data can be simulated in requestAction. Use $extra['url'] for
GET data. The $extra['data'] parameter allows POST data simulation.
- Parameters:
-
-
mixed $url required
String or array-based url. Unlike other url arrays in CakePHP, this url will not automatically handle passed and named arguments in the $url parameter.
-
array $extra optional array ( )
if array includes the key "return" it sets the AutoRender to true. Can also be used to submit GET/POST data, and named/passed arguments.
-
- Method defined in:
- Cake/Core/Object.php on line 66
- Return
mixed Boolean true or false on success/failure, or contents of rendered action if 'return' is set in $extra.
_set
topAllows setting of multiple properties of the object in a single line of code. Will only set properties that are part of a class declaration.
- Parameters:
-
-
array $properties optional array ( )
An associative array containing properties and corresponding values.
-
- Method defined in:
- Cake/Core/Object.php on line 164
- Return
void
set
topAllows a template or element to set a variable that will be available in a layout or other element. Analogous to Controller::set().
- Parameters:
-
-
mixed $one required
A string or an array of data.
-
mixed $two optional NULL
Value in case $one is a string (which then works as the key). Unused if $one is an associative array, otherwise serves as the values to $one's keys.
-
- Method defined in:
- Cake/View/View.php on line 764
- Return
void
__set
topMagic accessor for deprecated attributes.
- Parameters:
-
-
string $name required
Name of the attribute to set.
-
string $value required
Value of the attribute to set.
-
- Method defined in:
- Cake/View/View.php on line 815
- Return
mixed
start
topStart capturing output for a 'block'
- Parameters:
-
-
string $name required
The name of the block to capture for.
-
- Method defined in:
- Cake/View/View.php on line 617
- Return
void
- See
_stop
topStop execution of the current script. Wraps exit() making testing easier.
- Parameters:
-
-
integer|string $status optional 0
see http://php.net/exit for values
-
- Method defined in:
- Cake/Core/Object.php on line 137
- Return
void
toString
topObject-to-string conversion. Each class can override this method as necessary.
- Method defined in:
- Cake/Core/Object.php on line 41
- Return
string The name of this class
uuid
topGenerates a unique, non-random DOM ID for an object, based on the object type and the target URL.
- Parameters:
-
-
string $object required
Type of object, i.e. 'form' or 'link'
-
string $url required
The object's target URL
-
- Method defined in:
- Cake/View/View.php on line 743
- Return
string
