Class View
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. A special $content_for_layout
variable is available
in the layout, and it contains the rendered view. This also means you can pass data from the view to the
layout using $this->set()
-
Object
-
View
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.
mixed
Used to define methods a controller that will be cached.
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.
string
File extension. Defaults to Cake's template ".ctp".
boolean
True when the view has been rendered.
mixed
An array of names of built-in helpers to include.
string
Name of layout to use with this View.
string
Path to Layout.
string
Name of the controller.
string
Holds View output.
mixed
Current passed params
string
Name of the plugin.
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.
string
Sub-directory for this view file. This is often used for extension based routing.
Eg. With an xml
extension, $subDir would be xml/
string
Theme name. If you are using themes, you should remember to use ThemeView as well.
array
List of generated DOM UUIDs.
array
Holds current errors for the model validation.
string
Name of view to use with this View.
string
Path to View.
array
Variables for the view
Method Detail
__construct( Controller
$controller )
Constructor
Parameters
-
Controller
$controller
- A controller object to pull View::_passedVars from.
Overrides
__get( string $name )
Magic accessor for helpers. Provides access to attributes that were deprecated.
Parameters
-
string
$name
- Name of the attribute to get.
Returns
mixed
_getElementFileName( string $name , string $plugin = null )
Finds an element filename, returns false on failure.
Parameters
-
string
$name
- The name of the element to find.
-
string
$plugin
optional
null
- The plugin name the element is in.
Returns
mixed
Either a string to the element filename or false when one can't be found.
_getExtensions( )
Get the extensions that view files can use.
Returns
array
Array of extensions view files use.
_getLayoutFileName( string $name = null )
Returns layout filename for this template as a string.
Parameters
-
string
$name
optional
null
- The name of the layout to find.
Returns
string
Filename for layout file (.ctp).
Throws
_getViewFileName( string $name = null )
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.
Parameters
-
string
$name
optional
null
- Controller action to find template filename for
Returns
string
Template filename
Throws
_paths( string $plugin = null , boolean $cached = true )
Return 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.
Returns
array
paths
_render( string $___viewFn , array $___dataForView = array() )
Renders and returns output for given view filename with its
array of data.
Parameters
-
string
$___viewFn
- Filename of the view
-
array
$___dataForView
optional
array()
- Data to include in rendered view. If empty the current View::$viewVars will be used.
Returns
string
Rendered output
addScript( string $name , string $content = null )
Adds a script block or other element to be inserted in $scripts_for_layout in
the <head />
of a document layout
Parameters
-
string
$name
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.
element( string $name , array $data = array() , array $options = array() )
Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
This realizes the concept of Elements, (or "partial layouts") and the $params array is used to send
data to be used in the element. Elements can be cached improving performance by using the cache
option.
Parameters
-
string
$name
- Name of template file in the/app/View/Elements/ folder
-
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:
- 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.
- key
- Used to define the key used in the Cache::write(). It will be prefixed with element_
- plugin
- Load an element from a specific plugin.
- callbacks
- Set to true to fire beforeRender and afterRender helper callbacks for this element.
Defaults to false.
Returns
string
Rendered Element
getVar( string $var )
Returns the contents of the given View variable(s)
Parameters
-
string
$var
- The view var you want the contents of.
Returns
mixed
The content of the named var if its set, otherwise null.
getVars( )
Returns a list of variables available in the current View context
Returns
array
Array of the set view variable names.
loadHelper( string $helperName , array $settings = array() )
Loads a helper. Delegates to the HelperCollection::load()
to load the helper
Parameters
-
string
$helperName
- Name of the helper to load.
-
array
$settings
optional
array()
- Settings for the helper
Returns
Helper
a constructed helper object.
See
HelperCollection::load()
loadHelpers( )
Interact with the HelperCollection to load all the helpers.
render( string $view = null , string $layout = null )
Renders 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:
beforeRender
afterRender
beforeLayout
afterLayout
If View::$autoRender is false and no $layout
is provided, the view will be returned bare.
Parameters
-
string
$view
optional
null
- Name of view file to use
-
string
$layout
optional
null
- Layout to use.
Returns
string
Rendered Element
Throws
renderCache( string $filename , string $timeStart )
Render cached view. Works in concert with CacheHelper and Dispatcher to
render cached view files.
Parameters
-
string
$filename
- the cache file to include
-
string
$timeStart
- the page render start time
Returns
boolean
Success of rendering the cached file.
renderLayout( string $content_for_layout , string $layout = null )
Renders a layout. Returns output from _render(). Returns false on error.
Several variables are created for use in layout.
title_for_layout
- A backwards compatible place holder, you should set this value if you want more control.
content_for_layout
- contains rendered view file
scripts_for_layout
- contains scripts added to header
Parameters
-
string
$content_for_layout
- Content to render in a view, wrapped by the surrounding layout.
-
string
$layout
optional
null
- Layout name
Returns
mixed
Rendered output, or false on error
Throws
set( mixed $one , mixed $two = null )
Allows 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
- 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.
uuid( string $object , string $url )
Generates a unique, non-random DOM ID for an object, based on the object type and the target URL.
Parameters
-
string
$object
- Type of object, i.e. 'form' or 'link'
-
string
$url
- The object's target URL
Returns
string
Methods inherited from Object
_mergeVars( array $properties , string $class , boolean $normalize = true )
Merges 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
- The name of the properties to merge.
-
string
$class
- The class to merge the property with.
-
boolean
$normalize
optional
true
- Set to true to run the properties through Set::normalize() before merging.
_set( array $properties = array() )
Allows 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.
_stop( integer|string $status = 0 )
Stop execution of the current script. Wraps exit() making
testing easier.
Parameters
-
integer|string
$status
optional
0
- see http://php.net/exit for values
dispatchMethod( string $method , array $params = array() )
Calls a method on this object with the given parameters. Provides an OO wrapper
for call_user_func_array
Parameters
-
string
$method
- Name of the method to call
-
array
$params
optional
array()
- Parameter list to use when calling $method
Returns
mixed
Returns the result of the method call
log( string $msg , integer $type = LOG_ERROR )
Convenience method to write a message to CakeLog. See CakeLog::write()
for more information on writing to logs.
Parameters
-
string
$msg
- Log message
-
integer
$type
optional
LOG_ERROR
- Error type constant. Defined in app/Config/core.php.
Returns
boolean
Success of log write
requestAction( mixed $url , array $extra = array() )
Calls 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
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.
Returns
mixed
Boolean true or false on success/failure, or contents
of rendered action if 'return' is set in $extra.
toString( )
Object-to-string conversion.
Each class can override this method as necessary.
Returns
string
The name of this class