Cache

Implements \ArrayAccess

Lazy-loading template cache.

Group a set of templates together and only parse them on an as-needed basis.

api
package

Pablotron\Luigi

Methods

Create a new template cache.

__construct(array $templates, array $filters = array()) 
api

Example:

# load cache class
use \Pablotron\Luigi\Cache;

# create template cache
$cache = new Cache([
  'hi' => 'hi %{name}!',
]);

Arguments

$templates

array

Map of template keys to template strings.

$filters

array

Custom filter map (optional).

Returns true if the given template key exists in this cache.

offsetExists(string $key) : boolean
api

Example:

# load cache class
use \Pablotron\Luigi\Cache;

# create cache
$cache = new Cache([
  'hi' => 'hi %{name}!',
]);

# get template 'hi' from cache
foreach (array('hi', 'nope') as $tmpl_key) {
  echo "$key: " . (isset($cache[$key]) ? 'Yes' : 'No') . "\n"
}

# prints "hi: Yes" and "nope: No"

Arguments

$key

string

Template key.

Response

boolean

Returns true if the template key exists.

Get the template associated with the given template key.

offsetGet(string $key) : \Pablotron\Luigi\Template
api

Example:

# load cache class
use \Pablotron\Luigi\Cache;

# create cache
$cache = new Cache([
  'hi' => 'hi %{name}!',
]);

# get template 'hi' from cache
$tmpl = $cache['hi'];

echo $tmpl->run([
  'name' => 'Paul',
]);

# prints "hi Paul!"
Throws
\Pablotron\Luigi\UnknownTemplateError

if the given template does not exist.

Arguments

$key

string

Template key.

Response

\Pablotron\Luigi\Template

Returns template associated with this key.

Set the template associated with the given template key.

offsetSet(string $key, string $val) : void
api

Example:

# load cache class
use \Pablotron\Luigi\Cache;

# create cache
$cache = new Cache();

# add template to cache as 'hash-name'
$cache['hash-name'] = 'hashed name: %{name | hash sha1}';

echo $cache['hash-name']->run([
  'name' => 'Paul',
]);

# prints "sha1 of name: c3687ab9880c26dfe7ab966a8a1701b5e017c2ff"

Arguments

$key

string

Template key.

$val

string

Template string.

Remove the template associated with the given template key.

offsetUnset(array $key) : void
api

Example:

# load cache class
use \Pablotron\Luigi\Cache;

# create cache
$cache = new Cache([
  'hi' => 'hi %{name}!',
]);

# remove template 'hi' from cache
unset($cache['hi']);

echo $cache['hi']->run([
  'name' => 'Paul',
]);

# throws UnknownTemplateError

Arguments

$key

array

Template key.

Run template with the given arguments and return the result.

run(string $key, array $args = array()) : string
api

Example:

# load cache class
use \Pablotron\Luigi\Cache;

# create cache
$cache = new Cache([
  'my-template' => 'hello %{name | uc}',
]);

# run template
echo $cache->run('my-template', [
  'name' => 'Paul',
]);

# prints "hello PAUL"

Arguments

$key

string

Template key.

$args

array

Template arguments.

Response

string

Returns the expanded template result.

Properties

Map of keys to template strings.

templates : array
var

Map of keys to template strings.

Filter map (optional).

Parsed template cache.

Type(s)

array

Map of keys to template strings.

filters : array
var

Map of keys to template strings.

Filter map (optional).

Parsed template cache.

Type(s)

array

Map of keys to template strings.

lut : array
var

Map of keys to template strings.

Filter map (optional).

Parsed template cache.

Type(s)

array