aboutsummaryrefslogtreecommitdiff
path: root/php
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2018-09-05 00:36:39 -0400
committerPaul Duncan <pabs@pablotron.org>2018-09-05 00:36:39 -0400
commit4f92438ba4cbc04016e2b06996bb7d7e99e01952 (patch)
treeac34074bcada65b15e54bd63cea56653d597da79 /php
parent03dfd34bfb18907b8ff697d6b2f71623dd4998e8 (diff)
downloadluigi-template-4f92438ba4cbc04016e2b06996bb7d7e99e01952.tar.bz2
luigi-template-4f92438ba4cbc04016e2b06996bb7d7e99e01952.zip
php: add lots of Template examples
Diffstat (limited to 'php')
-rw-r--r--php/src/Template.php110
1 files changed, 110 insertions, 0 deletions
diff --git a/php/src/Template.php b/php/src/Template.php
index 34dfe09..47bcf5d 100644
--- a/php/src/Template.php
+++ b/php/src/Template.php
@@ -629,6 +629,116 @@ Filters::init();
*
* # 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
*
*/