Ejemplo n.º 1
0
<?php

require_once 'SithTemplate.php';
$environ = new TemplateEnviron();
// Context array is passed as first argument to Template::render, or as second
// argument to TemplateEnviron::render.
$tpl = $environ->get('string://{{ foo }} ');
echo $tpl->render(array('foo' => 'first'), $environ);
echo $tpl->render(array('foo' => 'second'), $environ);
// Will produce: "first second "
// Above is the simplest variable expression. To access nested elements, slightly more
// complex syntax is required, presented below, with equivalent PHP code:
//
// - accessing a named array element
//   {{ foo.bar }} is equivalent to $context['foo']['bar']
// - accessing a numeric array index
//   {{ foo.42 }} is equivalent to $context['foo'][42]
// - accessing a named or numeric array index, using value of another variable as key
//   {{ foo.[bar] }} is equivalent to $context['foo'][$context['bar']]
//
// Same syntax rules applies to object properties - you just use -> operator instead of ., e.g.
// {{ foo->bar }}.
//
// This syntax allows you to create very complex constructs, like:
//  {{ [one->[two]].three->four }} which is equivalent to
//  $context[ $context['one']->{$context['two']} ]['three']->four
//
// SithTemplate by default generates code to check whether variable really exists in the context
// before it is used, which triggers E_USER_WARNING if it doesn't. This can interfere with "optional"
// variables (e.g. ones used with 'default' filter). You can tell compiler to omit this code, by prefixing
// entire expression with @ sign:
Ejemplo n.º 2
0
<?php

require_once 'SithTemplate.php';
// 1. We create environment
$environ = new TemplateEnviron();
// 2. Next, we create template object
// Library will take care of the (re)compilation.
// SithTemplate 1.1 introduced unified I/O system,
// which allows you to easily inline small templates in your PHP code.
$template = $environ->get('string://Hello world');
// 3. Finally, we render and display previously created template
// You may notice that display/fetch APIs are gone, replaced by
// generic ones - you need to display template output by yourself.
//
// You can also see that environment object is passed back to the template -
// it is used in several places, like {% include %}-generated code, but passing
// it here, and not during construction, keeps template object more lightweight
// and independent, as it doesn't carry reference to original environment.
// It also eliminates possibility of circular reference, when template object
// is stored in environ's internal cache.
echo $template->render(array(), $environ);
// If you don't want to cache the template object on your own, you can use
// chained calls to cachedGet and render:
$environ->cachedGet('string://Other')->render(array(), $environ);
// If you don't need the object at all, you can call TemplateEnviron::render instead.
// This call is the same as the chained call above, just shorter and less explicit.
$environ->render('string://Other', array());