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)"
api
package

Pablotron\Luigi

Methods

Create a new Template object.

__construct(string $template, array $custom_filters = array()) 
api

Parse a template string into tokens.

Example:

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

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

Arguments

$template

string

Template string.

$custom_filters

array

Custom filter map (optional).

Return the input template string.

__toString() : string
api

Example:

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

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

# print template string
echo $tmpl . "\n";

# prints "hello %{name}"

Response

string

Input template string.

Parse template string, expand it using given arguments, and return the result as a string.

once(string $template, array $args = array(), array $filters = array()) : string
static api

Example:

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

# create template, run it, and print result
echo Template::once('foo-%{some-key}-bar', [
  'some-key' => 'example',
]) . "\n";

# prints "foo-example-bar"

Arguments

$template

string

Template string.

$args

array

Template arguments.

$filters

array

Custom filter map (optional).

Response

string

Expanded template.

Apply given arguments to template and return the result as a string.

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

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"
Throws
\Pablotron\Luigi\UnknownKeyError

If a referenced key is missing from $args.

Arguments

$args

array

Template arguments.

Response

string

Expanded template.

Properties

Input template string.

template : string
var

Input template string.

Type(s)

string

Filter map.

filters : array
var

Filter map.

Parsed template tokens.

Type(s)

array

Filter map.

tokens : array
var

Filter map.

Parsed template tokens.

Type(s)

array