Class StringTemplate
Provides an interface for registering and inserting content into simple logic-less string templates.
Used by several helpers to provide simple flexible templates for generating HTML and other content.
- Cake\View\StringTemplate uses Cake\Core\InstanceConfigTrait
Method Detail
__constructsource public
__construct( array $config [] )
Constructor.
Parameters
- array
$config
optional [] - A set of templates to add.
_compileTemplatessource protected
_compileTemplates( array $templates [] )
Compile templates into a more efficient printf() compatible format.
Parameters
- array
$templates
optional [] - The template names to compile. If empty all templates will be compiled.
_formatAttributesource protected
_formatAttribute( string $key , string|array $value , boolean $escape true )
Formats an individual attribute, and returns the string value of the composed attribute. Works with minimized attributes that have the same value as their name such as 'disabled' and 'checked'
Parameters
- string
$key
- The name of the attribute to create
- string|array
$value
- The value of the attribute to create.
- boolean
$escape
optional true - Define if the value must be escaped
Returns
string
The composed attribute.
addsource public
add( array $templates )
Registers a list of templates by name
Example:
$templater->add([ 'link' => '<a href="{{url}}">{{title}}</a>' 'button' => '<button>{{text}}</button>' ]);
Parameters
- array
$templates
- An associative list of named templates.
Returns
mixed
$this
formatsource public
format( string $name , array $data )
Format a template string with $data
Parameters
- string
$name
- The template name.
- array
$data
- The data to insert.
Returns
string|null
Formatted string or null if template not found.
formatAttributessource public
formatAttributes( array|null $options , array|null $exclude null )
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:
- '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.
Parameters
- array|null
$options
- Array of options.
- array|null
$exclude
optional null - Array of options to be excluded, the options here will not be part of the return.
Returns
string
Composed attributes.
loadsource public
load( string $file )
Load a config file containing templates.
Template files should define a $config
variable containing all the templates to load. Loaded templates will be merged with existing templates.
Parameters
- string
$file
- The file to load
removesource public
remove( string $name )
Remove the named template.
Parameters
- string
$name
- The template to remove.
Methods used from Cake\Core\InstanceConfigTrait
_configDeletesource protected
_configDelete( string $key )
Delete a single config key
Parameters
- string
$key
- Key to delete.
Throws
Cake\Core\Exception\Exception
if attempting to clobber existing config
_configReadsource protected
_configRead( string|null $key )
Read a config variable
Parameters
- string|null
$key
- Key to read.
Returns
mixed
mixed
_configWritesource protected
_configWrite( string|array $key , mixed $value , boolean|string $merge false )
Write a config variable
Parameters
- string|array
$key
- Key to write to.
- mixed
$value
- Value to write.
- boolean|string
$merge
optional false - True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Throws
Cake\Core\Exception\Exception
if attempting to clobber existing config
configsource public
config( string|array|null $key null , mixed|null $value null , boolean $merge true )
Usage
Reading the whole config:
$this->config();
Reading a specific value:
$this->config('key');
Reading a nested value:
$this->config('some.nested.key');
Setting a specific value:
$this->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
Parameters
- string|array|null
$key
optional null - The key to get/set, or a complete array of configs.
- mixed|null
$value
optional null - The value to set.
- boolean
$merge
optional true - Whether to recursively merge or overwrite existing config, defaults to true.
Returns
mixed
Config value being read, or the object itself on write operations.
Throws
Cake\Core\Exception\Exception
When trying to set a key that is invalid.
configShallowsource public
configShallow( string|array $key , mixed|null $value null )
Merge provided config with existing config. Unlike config()
which does a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
Parameters
- string|array
$key
- The key to set, or a complete array of configs.
- mixed|null
$value
optional null - The value to set.
Returns
mixed
$this The object itself.
getsource public
get( string|array|null $key null , mixed|null $value null , boolean $merge true )
Usage
Reading the whole config:
$this->config();
Reading a specific value:
$this->config('key');
Reading a nested value:
$this->config('some.nested.key');
Setting a specific value:
$this->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
Parameters
- string|array|null
$key
optional null - The key to get/set, or a complete array of configs.
- mixed|null
$value
optional null - The value to set.
- boolean
$merge
optional true - Whether to recursively merge or overwrite existing config, defaults to true.
Returns
mixed
Config value being read, or the object itself on write operations.
Throws
Cake\Core\Exception\Exception
When trying to set a key that is invalid.
Properties summary
Properties used from Cake\Core\InstanceConfigTrait
$_configInitializedsource
protected boolean
Whether the config property has already been configured with defaults
false
© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
http://api.cakephp.org/3.1/class-Cake.View.StringTemplate.html