Constants

VERSION

Current version of Luigi Template.

« More »

Classes, interfaces and traits

Cache

Lazy-loading template cache.

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

« More »

Filters

Static class containing global filter map.

The built-in default filters are:

  • h: HTML-escape input string (including quotes).
  • u: URL-escape input string.
  • json: JSON-encode input value.
  • hash: Hash input value. Requires hash algorithm as parameter.
  • base64: Base64-encode input string.
  • nl2br: Convert newlines to \<br/\> elements.
  • uc: Upper-case input string.
  • lc: Lower-case input string.
  • trim: Trim leading and trailing whitespace from input string.
  • ltrim: Trim leading whitespace from input string.
  • rtrim: Trim trailing whitespace from input string.
  • s: Return '' if input number is 1, and 's' otherwise (used for pluralization).
  • strlen: Return the length of the input string.
  • count: Return the number of elements in the input array.
  • key: Get the given key from the input array. Requires key as a parameter.

You can add your own filters to the default set of filters by modifying \Luigi\Filters::$FILTERS, like so:

# add a filter named "my-filter"
\Luigi\Filters['my-filter'] = function($s) {
  # filter input string
  return "foo-{$s}-bar";
};

# use custom filter in template
echo Template::once('%{some-key | my-filter}', [
  'some-key' => 'example',
]) . "\n";

# prints "foo-example-bar"
« More »

FilterToken

Filter parser token.

« More »

InvalidTemplateError

Thrown when trying to parse an invalid template string.

« More »

LiteralToken

Literal parser token.

« More »

LuigiError

Base class for all exceptions raised by Luigi Template.

« More »

MissingFilterParameterError

Thrown when trying to use a filter with with a missing parameter.

« More »

RunContext

Wrapper for context during Template#run().

« More »

Template

Template object.

Parse a template string into a Template instance, and then apply the Template via the Template#run() method.

Example:

# load template class
use \Pablotron\Luigi\Template;

# create template
$tmpl = new Template('hello %{name}');

# run template and print result
echo $tmpl->run([
  'name' => 'Paul',
]) . "\n";

# prints "hello Paul"

You can also filter values in templates, using the pipe symbol:

# create template that converts name to upper-case
$tmpl = new Template('hello %{name | uc}');

# run template and print result
echo $tmpl->run([
  'name' => 'Paul',
]) . "\n";

# prints "hello PAUL"

Filters can be chained:

# create template that converts name to upper-case and then
# strips leading and trailing whitespace
$tmpl = new Template('hello %{name | uc | trim}');

# run template and print result
echo $tmpl->run([
  'name' => '   Paul    ',
]) . "\n";

# prints "hello PAUL"

Filters can take arguments:

# create template that converts name to lowercase and then
# calculates the SHA-1 digest of the result
$tmpl = new Template('hello %{name | lc | hash sha1}');

# run template and print result
echo $tmpl->run([
  'name' => 'Paul',
]) . "\n";

# prints "hello a027184a55211cd23e3f3094f1fdc728df5e0500"

You can define custom global filters:

# load template and filter classes
use \Pablotron\Luigi\Template;
use \Pablotron\Luigi\Filters;

# create custom global filter named 'foobarify'
Filters::$FILTERS['foobarify'] = function($s) {
  return "foo-{$s}-bar";
};

# create template that converts name to lowercase and then
# calculates the SHA-1 digest of the result
$tmpl = new Template('hello %{name | foobarify}');

# run template and print result
echo $tmpl->run([
  'name' => 'Paul',
]) . "\n";

# prints "hello foo-Paul-bar"

Or define custom filters for a template:

# load template and filter classes
use \Pablotron\Luigi\Template;

# create template that converts name to lowercase and then
# calculates the SHA-1 digest of the result
$tmpl = new Template('hello %{name | reverse}', [);
  'reverse' => function($s) {
    return strrev($s);
  },
]);

# run template and print result
echo $tmpl->run([
  'name' => 'Paul',
]) . "\n";

# prints "hello luaP"

Your custom filters can accept arguments, too:

# load template and filter classes
use \Pablotron\Luigi\Template;
use \Pablotron\Luigi\Filters;

# create custom global filter named 'foobarify'
Filters::$FILTERS['wrap'] = function($s, $args) {
   if (count($args) == 2) {
     return sprintf('(%s, %s, %s)', $args[0], $s, $args[1]);
   } else if (count($args) == 1) {
     return sprintf('(%s in %s)', %s, $args[0]);
   } else if (count($args) == 0) {
     return $s;
   } else {
     throw new Exception("invalid argument count");
   }
};

# create template that converts name to lowercase and then
# calculates the SHA-1 digest of the result
$tmpl = new Template('sandwich: %{meat | wrap slice heel}, taco: %{meat | wrap shell}');

# run template and print result
echo $tmpl->run([
  'meat' => 'chicken',
]) . "\n";

# prints "sandwich: (slice, chicken, heel), taco: (chicken in shell)"
« More »

TemplateFilter

Parsed filter name and arguments.

« More »

Token

Abstract base class for parser tokens.

« More »

UnknownFilterError

Thrown when attempting to apply an unknown filter.

« More »

UnknownKeyError

Thrown when attempting to get an unknown key.

« More »

UnknownTemplateError

Thrown when attempting to get an unknown template from a Cache.

« More »

UnknownTypeError

Base class for all all unknown type errors.

« More »