PHP Class Cake\View\StringTemplate

Used by several helpers to provide simple flexible templates for generating HTML and other content.
Inheritance: use trait Cake\Core\InstanceConfigTrait
Datei anzeigen Open project: cakephp/cakephp Class Usage Examples

Protected Properties

Property Type Description
$_compactAttributes array List of attributes that can be made compact.
$_compiled array Contains the list of compiled templates
$_configStack array A stack of template sets that have been stashed temporarily.
$_defaultConfig array The default templates this instance holds.

Public Methods

Method Description
__construct ( array $config = [] ) Constructor.
add ( array $templates ) Registers a list of templates by name
format ( string $name, array $data ) : string | null Format a template string with $data
formatAttributes ( array | null $options, array | null $exclude = null ) : string Returns a space-delimited string with items of the $options array. If a key of $options array happens to be one of those listed in StringTemplate::$_compactAttributes and its value is one of:
get ( )
load ( string $file ) : void Load a config file containing templates.
pop ( ) : void Restore the most recently pushed set of templates.
push ( ) : void Push the current templates into the template stack.
remove ( string $name ) : void Remove the named template.

Protected Methods

Method Description
_compileTemplates ( array $templates = [] ) : void Compile templates into a more efficient printf() compatible format.
_formatAttribute ( string $key, string | array $value, boolean $escape = true ) : string Formats an individual attribute, and returns the string value of the composed attribute.

Method Details

__construct() public method

Constructor.
public __construct ( array $config = [] )
$config array A set of templates to add.

_compileTemplates() protected method

Compile templates into a more efficient printf() compatible format.
protected _compileTemplates ( array $templates = [] ) : void
$templates array The template names to compile. If empty all templates will be compiled.
return void

_formatAttribute() protected method

Works with minimized attributes that have the same value as their name such as 'disabled' and 'checked'
protected _formatAttribute ( string $key, string | array $value, boolean $escape = true ) : string
$key string The name of the attribute to create
$value string | array The value of the attribute to create.
$escape boolean Define if the value must be escaped
return string The composed attribute.

add() public method

### Example: $templater->add([ 'link' => '{{title}}' 'button' => '' ]);
public add ( array $templates )
$templates array An associative list of named templates.

format() public method

Format a template string with $data
public format ( string $name, array $data ) : string | null
$name string The template name.
$data array The data to insert.
return string | null Formatted string or null if template not found.

formatAttributes() public method

- '1' (string) - 1 (integer) - true (boolean) - 'true' (string) Then the value will be reset to be identical with key's name. If the value is not one of these 4, the parameter is not output. 'escape' is a special option in that it controls the conversion of attributes to their HTML-entity encoded equivalents. Set to false to disable HTML-encoding. If value for any option key is set to null or false, that option will be excluded from output. This method uses the 'attribute' and 'compactAttribute' templates. Each of these templates uses the name and value variables. You can modify these templates to change how attributes are formatted.
public formatAttributes ( array | null $options, array | null $exclude = null ) : string
$options array | null Array of options.
$exclude array | null Array of options to be excluded, the options here will not be part of the return.
return string Composed attributes.

get() public method

public get ( )

load() public method

Template files should define a $config variable containing all the templates to load. Loaded templates will be merged with existing templates.
public load ( string $file ) : void
$file string The file to load
return void

pop() public method

Restore the most recently pushed set of templates.
public pop ( ) : void
return void

push() public method

Push the current templates into the template stack.
public push ( ) : void
return void

remove() public method

Remove the named template.
public remove ( string $name ) : void
$name string The template to remove.
return void

Property Details

$_compactAttributes protected_oe property

List of attributes that can be made compact.
protected array $_compactAttributes
return array

$_compiled protected_oe property

Contains the list of compiled templates
protected array $_compiled
return array

$_configStack protected_oe property

A stack of template sets that have been stashed temporarily.
protected array $_configStack
return array

$_defaultConfig protected_oe property

The default templates this instance holds.
protected array $_defaultConfig
return array