PHP Class lithium\template\View

The View class interacts with a variety of other classes in order to achieve maximum flexibility and configurability at all points in the view rendering and presentation process. The Loader class is tasked with locating and reading template files which are then passed to the Renderer adapter subclass. In the default configuration, the File adapter acts as both renderer and loader, loading files from paths defined in _process steps_ (described below) and rendering them as plain PHP files, augmented with special syntax. The View class operates on _processes_, which define the steps to render a completed view. For example, the default process, which renders a template wrapped in a layout, is comprised of two _steps_: the first step renders the main template and captures it to the rendering context, where it is embedded in the layout in the second step. See the $_steps and $_processes properties for more information. Using steps and processes, you can create rendering scenarios to suit very complex needs. By default, the View class is called during the course of the framework's dispatch cycle by the Media class. However, it is also possible to instantiate and call View directly, in cases where you wish to bypass all other parts of the framework and simply return rendered content. A simple example, using the Simple renderer/loader for string templates: {{{ $view = new View(array('loader' => 'Simple', 'renderer' => 'Simple')); echo $view->render('element', array('name' => "Robert"), array('element' => 'Hello, {:name}!')); Output: "Hello, Robert!"; }}} _Note_: This is easily adapted for XML templating. Another example, this time of something that could be used in an appliation error handler: {{{ $view = new View(array( 'paths' => array( 'template' => '{:library}/views/errors/{:template}.{:type}.php', 'layout' => '{:library}/views/layouts/{:layout}.{:type}.php', ) )); $page = $View->render('all', array('content' => $info), array( 'template' => '404', 'layout' => 'error' )); }}} To learn more about processes and process steps, see the $_processes and $_steps properties, respectively.
See also: lithium\template\view\Renderer
See also: lithium\template\view\adapter
See also: lithium\net\http\Media
Inheritance: extends lithium\core\Object
Show file Open project: unionofrad/lithium Class Usage Examples

Public Properties

Property Type Description
$outputFilters Output filters for view rendering.

Protected Properties

Property Type Description
$_adapters string Path used to look up view loading and rendering adapters.
$_autoConfig Auto-configuration parameters.
$_loader The object responsible for loading template files.
$_processes array For example, the default process, 'all', renders a template, then renders a layout, using the rendered template content. A process can be defined using one or more steps defined in the $_steps property. Each process definition is a simple array of ordered values, where each value is a key in the $_steps array.
$_renderer Object responsible for rendering output.
$_request Holds the details of the current request that originated the call to this view, if applicable. May be empty if this does not apply. For example, if the View class is created to render an email.
$_response Holds a reference to the Response object that will be returned at the end of the current dispatch cycle. Allows headers and other response attributes to be assigned in the templating layer.
$_steps array Each step is named by its key in the $_steps array, and can have the following options: - 'path' _string_: Indicates the set of paths to use when loading templates. - 'conditions' _mixed_: Make the step dependent on a value being present, or on some other arbitrary condition. If a 'conditions' is a string, it indicates that a key with that name must be present in the $options passed to render(), and must be set to a non-empty value. If a closure, it will be executed with the rendering parameters, and must return true or false. In either case, if the condition is satisfied, the step is processed. Otherwise, it is skipped. See the _conditions() method for more information. - 'capture' _array_: If specified, allows the results of this rendering step to be assigned to a template variable used in subsequent steps, or to the templating context for use in subsequent steps. If can be specified in the form of array('context' => '') or array('data' => ''). If the 'context' key is used, the results are captured to the rendering context. Likewise with the 'data' key, results are captured to a template variable. - 'multi' _boolean_: If set to true, the rendering parameter matching the name of this step can be an array containing multiple values, in which case this step is executed multiple times, once for each value of the array.

Public Methods

Method Description
__construct ( array $config = [] ) : void Constructor.
render ( string $process, array $data = [], array $options = [] ) : string Executes a named rendering process by running each process step in sequence and aggregating the results. The View class comes with 3 built-in processes: 'all', 'template', and 'element'. The 'all' process is the default two-step rendered view, where a template is wrapped in a layout containing a header and footer.

Protected Methods

Method Description
_conditions ( array $step, array $params, array $data, array $options ) : boolean Evaluates a step condition to determine if the step should be executed.
_convertSteps ( array $command, array &$params, array $defaults ) : array Handles API backward compatibility by converting an array-based rendering instruction passed to render() as a process, to a set of rendering steps, rewriting any associated rendering parameters as necessary.
_init ( ) : void Perform initialization of the View.
_process ( string $process, array &$params ) : array Converts a process name to an array containing the rendering steps to be executed for each process.
_step ( array $step, array $params, array &$data, array &$options = [] ) : string Performs a rendering step.

Method Details

__construct() public method

Constructor.
See also: lithium\template\View::$_steps
See also: lithium\template\View::$_processes
public __construct ( array $config = [] ) : void
$config array Class configuration parameters The available options are: - `'loader'` _mixed_: Instance or name of the class used for locating and reading template content. Defaults to `File`, which reads template content from PHP files. - `'renderer'` _mixed_: Instance or name of the class that populates template content with the data passed in to the view layer, typically from a controller. Defaults to `'File'`, which executes templates as standard PHP files, using path information returned from the `loader` class. Both `loader` and `renderer` classes are looked up using the `'adapter.template.view'` path, which locates classes in the `extensions\adapter\template\view` sub-namespace of an application or plugin. - `'request'`: The `Request` object to be made available in the templates. Defaults to `null`. - `'steps'` _array_: The array of step configurations to add to the built-in configurations. Will be merged with the defaults, with any configurations passed in overwriting built-in steps. See the `$_steps` property for more information. - `'processes'` _array_: The array of process steps to add to the built-in configurations. Will be merged with the defaults, with any configurations passed in overwriting built-in processes. See the `$_processes` property for more information. - `'outputFilters'` _array_: An array of filters to be used when handling output. By default, the class is initialized with one filter, `h`, which is used in automatic output escaping.
return void

_conditions() protected method

Evaluates a step condition to determine if the step should be executed.
See also: lithium\template\View::$_steps
protected _conditions ( array $step, array $params, array $data, array $options ) : boolean
$step array The array of instructions that define a rendering step.
$params array The parameters associated with this rendering operation, as passed to `render()` (filtered from the `$options` parameter).
$data array The associative array of template variables passed to `render()`.
$options array The `$options` parameter, as passed to `render()`.
return boolean Returns `true` if the step should be executed, or `false` if the step should be skipped. If the step array has a `'conditions'` key which is a string, it checks to see if the rendering options (`$options`) contain a key of the same name, and if that key evaluates to `true`. If `'conditions'` is a closure, that closure is executed with the rendering parameters (`$params`, `$data`, and `$options`), and the result is determined by the return value of the closure. If a step definition has no `'conditions'` key, it is always executed.

_convertSteps() protected method

Handles API backward compatibility by converting an array-based rendering instruction passed to render() as a process, to a set of rendering steps, rewriting any associated rendering parameters as necessary.
protected _convertSteps ( array $command, array &$params, array $defaults ) : array
$command array A deprecated rendering instruction, i.e. `array('template' => '/path/to/template')`.
$params array The array of associated rendering parameters, passed by reference.
$defaults array Default step rendering options to be merged with the passed rendering instruction information.
return array Returns a converted set of rendering steps, to be executed in `render()`.

_init() protected method

Looks up and initializes loader and renderer classes, and initializes the output escape handler, matching the encoding from the Response object.
protected _init ( ) : void
return void

_process() protected method

Converts a process name to an array containing the rendering steps to be executed for each process.
protected _process ( string $process, array &$params ) : array
$process string A named set of rendering steps.
$params array
return array A 2-dimensional array that defines the rendering process. The first dimension is a numerically-indexed array containing each rendering step. The second dimension represents the parameters for each step.

_step() protected method

Performs a rendering step.
See also: lithium\template\view\adapter\File::template()
See also: lithium\template\view\Renderer::render()
See also: lithium\template\view\adapter\File::render()
protected _step ( array $step, array $params, array &$data, array &$options = [] ) : string
$step array The array defining the step configuration to render.
$params array An associative array of string values used in the template lookup process. See the `$params` argument of `File::template()`.
$data array associative array for template data.
$options array An associative array of options to pass to the renderer. See the `$options` parameter of `Renderer::render()` or `File::render()`.
return string

render() public method

Executes a named rendering process by running each process step in sequence and aggregating the results. The View class comes with 3 built-in processes: 'all', 'template', and 'element'. The 'all' process is the default two-step rendered view, where a template is wrapped in a layout containing a header and footer.
See also: lithium\template\View::_conditions()
See also: lithium\template\View::$_processes
See also: lithium\template\View::$_steps
public render ( string $process, array $data = [], array $options = [] ) : string
$process string A named set of rendering steps defined in the `$_processes` array.
$data array An associative array of data to be rendered in the set of templates.
$options array Options used when rendering. Available keys are as follows: - `'type'` _string_: The type of content to render. Defaults to `'html'`. - `'layout'` _string_: The name of the layout to use in the default two-step rendering process. Defaults to `null`. - `'template'` _string_: The name of the template to render. Defaults to `null`. - `'context'` _array_: An associative array of information to inject into the rendering context. - `'paths'` _array_: A nested array of paths to use for rendering steps. The top-level keys should match the `'path'` key in a step configuration (i.e.: `'template'`, `'layout'`, or `'element'`), and the second level is an array of path template strings to search (can be a string if there's only one path). These path strings generally take the following form: `'{:library}/views/{:controller}/{:template}.{:type}.php'`. These template strings are specific to the `File` loader, but can take any form useful to the template loader being used.
return string Returns the result of the rendering process, typically by rendering a template first, then rendering a layout (using the default configuration of the `'all'` process).

Property Details

$_adapters protected property

Path used to look up view loading and rendering adapters.
protected string $_adapters
return string

$_autoConfig protected property

Auto-configuration parameters.
protected $_autoConfig

$_loader protected property

The object responsible for loading template files.
protected $_loader

$_processes protected property

For example, the default process, 'all', renders a template, then renders a layout, using the rendered template content. A process can be defined using one or more steps defined in the $_steps property. Each process definition is a simple array of ordered values, where each value is a key in the $_steps array.
See also: lithium\template\View::$_steps
See also: lithium\template\View::render()
protected array $_processes
return array

$_renderer protected property

Object responsible for rendering output.
protected $_renderer

$_request protected property

Holds the details of the current request that originated the call to this view, if applicable. May be empty if this does not apply. For example, if the View class is created to render an email.
See also: lithium\action\Request
protected $_request

$_response protected property

Holds a reference to the Response object that will be returned at the end of the current dispatch cycle. Allows headers and other response attributes to be assigned in the templating layer.
See also: lithium\action\Response
protected $_response

$_steps protected property

Each step is named by its key in the $_steps array, and can have the following options: - 'path' _string_: Indicates the set of paths to use when loading templates. - 'conditions' _mixed_: Make the step dependent on a value being present, or on some other arbitrary condition. If a 'conditions' is a string, it indicates that a key with that name must be present in the $options passed to render(), and must be set to a non-empty value. If a closure, it will be executed with the rendering parameters, and must return true or false. In either case, if the condition is satisfied, the step is processed. Otherwise, it is skipped. See the _conditions() method for more information. - 'capture' _array_: If specified, allows the results of this rendering step to be assigned to a template variable used in subsequent steps, or to the templating context for use in subsequent steps. If can be specified in the form of array('context' => '') or array('data' => ''). If the 'context' key is used, the results are captured to the rendering context. Likewise with the 'data' key, results are captured to a template variable. - 'multi' _boolean_: If set to true, the rendering parameter matching the name of this step can be an array containing multiple values, in which case this step is executed multiple times, once for each value of the array.
See also: lithium\template\View::$_processes
See also: lithium\template\View::render()
protected array $_steps
return array

$outputFilters public property

Output filters for view rendering.
public $outputFilters