public function testModifiers() { // TODO: Test all modifiers Tonic::extendModifier("myModifier", function ($input, $prepend, $append = "") { if (empty($prepend)) { throw new \InvalidArgumentException("prepend is required"); } return $prepend . $input . $append; }); $tpl = array(array('<p>{$name.upper()}</p>', '<p>RICARDO</p>'), array('<p>{$array.one.capitalize().truncate(10)}</p>', '<p>First Item...</p>'), array('<p>{$empty.default("No value")}</p>', '<p>No value</p>'), array('<p>{$date.date("d-m-Y")}</p>', '<p>30-11-2016</p>'), array('<p>{$name.myModifier("Mr. ")}</p>', '<p>Mr. Ricardo</p>')); foreach ($tpl as $i => $t) { $template = new Tonic(); $template->loadFromString($t[0])->setContext($this->vars); $this->assertEquals($t[1], $template->render()); } }
namespace main; require_once "../src/Tonic.php"; use Tonic\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