Fr3nch13/CakePHP Utilities

StringTemplate
in package
uses InstanceConfigTrait

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.

Table of Contents

Properties

$_compactAttributes  : array<string, bool>
List of attributes that can be made compact.
$_compiled  : array<string, array<string|int, mixed>>
Contains the list of compiled templates
$_config  : array<string, mixed>
Runtime config
$_configInitialized  : bool
Whether the config property has already been configured with defaults
$_configStack  : array<string|int, mixed>
A stack of template sets that have been stashed temporarily.
$_defaultConfig  : array<string, mixed>
The default templates this instance holds.

Methods

__construct()  : mixed
Constructor.
add()  : $this
Registers a list of templates by name
addClass()  : array<string|int, string>|string
Adds a class and returns a unique list either in array or space separated
configShallow()  : $this
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
format()  : string
Format a template string with $data
formatAttributes()  : 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:
getConfig()  : mixed
Returns the config.
getConfigOrFail()  : mixed
Returns the config for this specific key.
load()  : 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()  : void
Remove the named template.
setConfig()  : $this
Sets the config.
_compileTemplates()  : void
Compile templates into a more efficient printf() compatible format.
_configDelete()  : void
Deletes a single config key.
_configRead()  : mixed
Reads a config key.
_configWrite()  : void
Writes a config key.
_formatAttribute()  : string
Formats an individual attribute, and returns the string value of the composed attribute.

Properties

$_compactAttributes

List of attributes that can be made compact.

protected array<string, bool> $_compactAttributes = ['allowfullscreen' => true, 'async' => true, 'autofocus' => true, 'autoplay' => true, 'checked' => true, 'compact' => true, 'controls' => true, 'declare' => true, 'default' => true, 'defaultchecked' => true, 'defaultmuted' => true, 'defaultselected' => true, 'defer' => true, 'disabled' => true, 'enabled' => true, 'formnovalidate' => true, 'hidden' => true, 'indeterminate' => true, 'inert' => true, 'ismap' => true, 'itemscope' => true, 'loop' => true, 'multiple' => true, 'muted' => true, 'nohref' => true, 'noresize' => true, 'noshade' => true, 'novalidate' => true, 'nowrap' => true, 'open' => true, 'pauseonexit' => true, 'readonly' => true, 'required' => true, 'reversed' => true, 'scoped' => true, 'seamless' => true, 'selected' => true, 'sortable' => true, 'truespeed' => true, 'typemustmatch' => true, 'visible' => true]

$_compiled

Contains the list of compiled templates

protected array<string, array<string|int, mixed>> $_compiled = []

$_configInitialized

Whether the config property has already been configured with defaults

protected bool $_configInitialized = false

$_configStack

A stack of template sets that have been stashed temporarily.

protected array<string|int, mixed> $_configStack = []

$_defaultConfig

The default templates this instance holds.

protected array<string, mixed> $_defaultConfig = []

Methods

__construct()

Constructor.

public __construct([array<string, mixed> $config = [] ]) : mixed
Parameters
$config : array<string, mixed> = []

A set of templates to add.

add()

Registers a list of templates by name

public add(array<string|int, string> $templates) : $this

Example:

$templater->add([
  'link' => '<a href="{{url}}">{{title}}</a>'
  'button' => '<button>{{text}}</button>'
]);
Parameters
$templates : array<string|int, string>

An associative list of named templates.

Return values
$this

addClass()

Adds a class and returns a unique list either in array or space separated

public addClass(array<string|int, mixed>|string $input, array<string|int, string>|string $newClass[, string $useIndex = 'class' ]) : array<string|int, string>|string
Parameters
$input : array<string|int, mixed>|string

The array or string to add the class to

$newClass : array<string|int, string>|string

the new class or classes to add

$useIndex : string = 'class'

if you are inputting an array with an element other than default of 'class'.

Return values
array<string|int, string>|string

configShallow()

Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.

public configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this

Setting a specific value:

$this->configShallow('key', $value);

Setting a nested value:

$this->configShallow('some.nested.key', $value);

Updating multiple config settings at the same time:

$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
$key : array<string, mixed>|string

The key to set, or a complete array of configs.

$value : mixed|null = null

The value to set.

Return values
$this

format()

Format a template string with $data

public format(string $name, array<string, mixed> $data) : string
Parameters
$name : string

The template name.

$data : array<string, mixed>

The data to insert.

Tags
throws
RuntimeException

If template not found.

Return values
string

Formatted string

formatAttributes()

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:

public formatAttributes(array<string, mixed>|null $options[, array<string|int, string>|null $exclude = null ]) : string
  • '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
$options : array<string, mixed>|null

Array of options.

$exclude : array<string|int, string>|null = null

Array of options to be excluded, the options here will not be part of the return.

Return values
string

Composed attributes.

getConfig()

Returns the config.

public getConfig([string|null $key = null ][, mixed $default = null ]) : mixed

Usage

Reading the whole config:

$this->getConfig();

Reading a specific value:

$this->getConfig('key');

Reading a nested value:

$this->getConfig('some.nested.key');

Reading with default value:

$this->getConfig('some-key', 'default-value');
Parameters
$key : string|null = null

The key to get or null for the whole config.

$default : mixed = null

The return value when the key does not exist.

Return values
mixed

Configuration data at the named key or null if the key does not exist.

getConfigOrFail()

Returns the config for this specific key.

public getConfigOrFail(string $key) : mixed

The config value for this key must exist, it can never be null.

Parameters
$key : string

The key to get.

Tags
throws
InvalidArgumentException
Return values
mixed

Configuration data at the named key

load()

Load a config file containing templates.

public load(string $file) : void

Template files should define a $config variable containing all the templates to load. Loaded templates will be merged with existing templates.

Parameters
$file : string

The file to load

pop()

Restore the most recently pushed set of templates.

public pop() : void

push()

Push the current templates into the template stack.

public push() : void

remove()

Remove the named template.

public remove(string $name) : void
Parameters
$name : string

The template to remove.

setConfig()

Sets the config.

public setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this

Usage

Setting a specific value:

$this->setConfig('key', $value);

Setting a nested value:

$this->setConfig('some.nested.key', $value);

Updating multiple config settings at the same time:

$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
$key : array<string, mixed>|string

The key to set, or a complete array of configs.

$value : mixed|null = null

The value to set.

$merge : bool = true

Whether to recursively merge or overwrite existing config, defaults to true.

Tags
throws
CakeException

When trying to set a key that is invalid.

Return values
$this

_compileTemplates()

Compile templates into a more efficient printf() compatible format.

protected _compileTemplates([array<string|int, string> $templates = [] ]) : void
Parameters
$templates : array<string|int, string> = []

The template names to compile. If empty all templates will be compiled.

_configDelete()

Deletes a single config key.

protected _configDelete(string $key) : void
Parameters
$key : string

Key to delete.

Tags
throws
CakeException

if attempting to clobber existing config

_configRead()

Reads a config key.

protected _configRead(string|null $key) : mixed
Parameters
$key : string|null

Key to read.

_configWrite()

Writes a config key.

protected _configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
$key : array<string, mixed>|string

Key to write to.

$value : mixed

Value to write.

$merge : string|bool = false

True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.

Tags
throws
CakeException

if attempting to clobber existing config

_formatAttribute()

Formats an individual attribute, and returns the string value of the composed attribute.

protected _formatAttribute(string $key, array<string|int, string>|string $value[, bool $escape = true ]) : string

Works with minimized attributes that have the same value as their name such as 'disabled' and 'checked'

Parameters
$key : string

The name of the attribute to create

$value : array<string|int, string>|string

The value of the attribute to create.

$escape : bool = true

Define if the value must be escaped

Return values
string

The composed attribute.


        
On this page

Search results