/**
  setUp is called before every test case, to setup a clean environment.
 */
 public function setUp()
 {
     @mkdir('./data/tplc');
     @mkdir($outputPrefix = './data/tplc/' . phpversion() . '/');
     $this->environ = TemplateEnviron::createFromINI('./data/conf.ini');
     $this->environ->settings['outputPrefix'] = $outputPrefix;
     $this->compiler = new TemplateCompilerEx();
     $this->compiler->settings =& $this->environ->settings;
     $this->environ->compiler = $this->compiler;
     $this->skipReflection = array('method' => array(), 'argument' => array());
     $this->skipSPL = array();
     if (version_compare(PHP_VERSION, '5.2.0', '<')) {
         $this->skipReflection['argument'] = array('getPosition');
         $this->skipSPL = array('objectHash');
     }
     $this->compiler->reset();
 }
<?php

require_once 'SithTemplate.php';
$environ = new TemplateEnviron();
// All security settings are set using environment's setting array.
// Some of them may be enforced at runtime, and some at compile time,
// see TemplateEnviron::$settings documentation for reference.
// The most common is variable autoescaping, which applies "escape" filter
// to all stand-alone variables (i.e. {{ vars }}), unless they are marked
// with "safe" pseudofilter.
// Autoescaping is turned on with "autoEscape" boolean setting.
$environ->settings['autoEscape'] = true;
$environ->render('string://{{ var }}', array('var' => '<b>'));
// will return "&lt;b&gt;"
$environ->render('string://{{ var|safe }}', array('var' => '<b>'));
// will return "<b>"
// Next, there are I/O restriction settings. They allow you to enforce specific I/O driver,
// e.g. when you load template using your own db:// driver, and you don't want loaded template
// to use any other I/O driver, like file:// or string://.
// Note that this is a bit primitive, and may be replaced sometime in the future.
// I/O restrictions are turned on by "restrictIncludeIO" and "restrictExtendIO" boolean settings.
$environ->settings['restrictIncludeIO'] = true;
$environ->render('string://{% include "string://test" %}', array());
// will return "test"
$environ->render('string://{% include "file://test.html" %}', array());
// will raise TemplateError
// Next, there are {{ internal }} access restrictions (again, a bit primitive and boolean only).
// Since {{ internal }} allows template to access global constants and superglobal arrays
// (like $_SERVER or $_ENV), it may introduce security risk in sandboxed environment
// (e.g. when templates are loaded from DB, and users can edit them).
// {{ internal }} restrictions can be set by turning off "allowInternalRequest"
<?php

require_once 'SithTemplate.php';
// You can change default settings during TemplateEnviron construction,
// by passing associative array to the constructor.
$environ = new TemplateEnviron(array('inputPrefix' => './templates/', 'outputPrefix' => './templates_c/'));
// You can also load settings from INI file, using static named constructor
// See sample-configuration.ini for syntax.
$environ = TemplateEnviron::createFromINI('settings.ini');
// Finally, you can change settings in runtime, by modifying settings
// array directly. Note that some settings won't take effect if changed
// in that way. Refer to documentation for more information.
$environ->settings['recompilationMode'] = TemplateEnviron::RECOMPILE_ALWAYS;
<?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:
<?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());
<?php

require_once 'SithTemplate.php';
$environ = new TemplateEnviron();
// You should always remember about error handling
// If error occurs during template compilation, exception message
// may contain template file and approx. line of the mistake.
// Errors are grouped - every group has it's own errorcode, specified
// as class constants in TemplateError.
try {
    $environ->render('string://{% bkock foo %}Typos are evil.{% endblock %}', array());
} catch (TemplateError $e) {
    echo $e->getMessage();
    // Unknown tag ...
    echo $e->getCode();
    // TemplateError::E_UNKNOWN_TAG
}