예제 #1
0
파일: TonicTest.php 프로젝트: rgamba/tonic
 public function testContextAwareness()
 {
     $tpl = array(array('<h1 data-attr="{$inline_js}">Test</h1>', '<h1 data-attr="javascript%3A+alert%28%27Hello%27%29%3B">Test</h1>'), array('<pre>{$inline_js}</pre>', '<pre>javascript: alert(&#039;Hello&#039;);</pre>'), array('<a href="{$inline_js}">Click</a>', '<a href="javascript%3A+alert%28%27Hello%27%29%3B">Click</a>'), array('<a href="?{$array}">Link</a>', '<a href="?one=first+item+string&two=2">Link</a>'), array('<p>{$inline_js.ignoreContext()}</p>', '<p>javascript: alert(\'Hello\');</p>'));
     foreach ($tpl as $i => $t) {
         $template = new Tonic();
         $template->loadFromString($t[0])->setContext($this->vars);
         $this->assertEquals($t[1], $template->render());
     }
 }
예제 #2
0
파일: demo.php 프로젝트: rgamba/tonic
// Set the local timezone
Tonic::$local_tz = "America/Mexico_city";
Tonic::$context_aware = true;
// This variables will be available in all the templates
Tonic::setGlobals(array("now" => @date_create(), "context" => array("post" => $_POST, "get" => $_GET)));
// Create a custom modifier
Tonic::extendModifier("myModifier", function ($input, $prepend, $append = "") {
    // $input will hold the current variable value, it's mandatory that your lambda
    // function has an input receiver, all other arguments are optional
    // We can perform input validations
    if (empty($prepend)) {
        throw new \InvalidArgumentException("prepend is required");
    }
    return $prepend . $input . $append;
});
$tpl = new Tonic("demo.html");
// Uncomment the following 2 lines to enable caching
//$tpl->enable_content_cache = true;
//$tpl->cache_dir = './cache/';
// Assign a variable to the template
$tpl->user_role = "member";
// Another method to assign variables:
$tpl->assign("currency", "USD");
// Assign arrays to the template
$tpl->user = array("name" => "Ricardo", "last_name" => "Gamba", "email" => "*****@*****.**", "extra" => "This is a large description of the user");
// Assign a more complex array
$tpl->users = array(array("name" => "rocio 'lavin'", "email" => "*****@*****.**", "role" => "admin"), array("name" => "roberto lopez", "email" => "*****@*****.**", "role" => "member"), array("name" => "rodrigo gomez", "email" => "*****@*****.**", "role" => "member"));
$tpl->number = 10;
$tpl->js = '{"name" : "Ricardo", "last_name": "Gamba"}';
$tpl->array = array("name" => "Ricardo", "last_name" => "Gamba");
$tpl->js_text = "Ricardo";