/** * Renders a handlebars template specified by $path, using scope variables defined inside $locals. * This function uses config('dispatch.views') for the location of the templates and partials, unless overridden by config('handlebars.views'). * Settings for 'handlebars.charset' and 'handlebars.layout' are also pulled from config(), if present. * * @param string $path name of the .handlebars file to render * @param array $locals scope variables to load inside the template * @param string $layout layout file to use, or flag to not use one * * @return string rendered code for the template */ function handlebars_template($path, $locals = array()) { static $engine = null; // create the engine once if (!$engine) { $views_path = config('handlebars.views') ?: config('dispatch.views'); // // Handlebars // $opts = array('loader' => new \Handlebars\Loader\FilesystemLoader($views_path), 'partials_loader' => new \Handlebars\Loader\FilesystemLoader($views_path, array('prefix' => config('handlebars.partials_prefix') ?: '_')), 'charset' => config('handlebars.charset') ?: 'UTF-8'); if ($cache_path = config('handlebars.cache')) { $opts['cache'] = $cache_path; } $engine = new Handlebars\Handlebars($opts); // // Handlebars Helpers // $helpers = config('handlebars.helpers'); if ($helpers) { foreach ($helpers as $helper => $callback) { $engine->addHelper($helper, function ($template, $context, $args, $source) use($callback) { return call_user_func($callback, $template, $context, $args, $source); }); } } $engine->addHelper('capitalize', function ($template, $context, $args, $source) { return ucwords($context->get($args)); }); $engine->addHelper('upper', function ($template, $context, $args, $source) { return strtoupper($context->get($args)); }); $engine->addHelper('lower', function ($template, $context, $args, $source) { return strtolower($context->get($args)); }); $engine->addHelper('url', function ($template, $context, $args, $source) { return config('dispatch.url') . $context->get($args); }); } // render partial using $locals if (config('handlebars.minify')) { return handlebars_minify($engine->render($path, $locals)); } return $engine->render($path, $locals); }
/** * Helper subexpressions test */ public function testHelperSubexpressions() { $loader = new \Handlebars\Loader\StringLoader(); $engine = new \Handlebars\Handlebars(array('loader' => $loader)); $engine->addHelper('test', function ($template, $context, $args) { $parsedArgs = $template->parseArguments($args); return (count($parsedArgs) ? $context->get($parsedArgs[0]) : '') . 'Test.'; }); // assert that nested syntax is accepted and sub-helper is run $this->assertEquals('Test.Test.', $engine->render('{{test (test)}}', array())); $engine->addHelper('add', function ($template, $context, $args) { $sum = 0; foreach ($template->parseArguments($args) as $value) { $sum += intval($context->get($value)); } return $sum; }); // assert that subexpression result is inserted correctly as argument to top level helper $this->assertEquals('42', $engine->render('{{add 21 (add 10 (add 5 6))}}', array())); // assert that bracketed expressions within string literals are treated correctly $this->assertEquals("(test)Test.", $engine->render("{{test '(test)'}}", array())); $this->assertEquals(")Test.Test.", $engine->render("{{test (test ')')}}", array())); $engine->addHelper('concat', function (\Handlebars\Template $template, \Handlebars\Context $context, $args) { $result = ''; foreach ($template->parseArguments($args) as $arg) { $result .= $context->get($arg); } return $result; }); $this->assertEquals('ACB', $engine->render('{{concat a (concat c b)}}', array('a' => 'A', 'b' => 'B', 'c' => 'C'))); $this->assertEquals('ACB', $engine->render('{{concat (concat a c) b}}', array('a' => 'A', 'b' => 'B', 'c' => 'C'))); $this->assertEquals('A-B', $engine->render('{{concat (concat a "-") b}}', array('a' => 'A', 'b' => 'B'))); $this->assertEquals('A-B', $engine->render('{{concat (concat a "-") b}}', array('a' => 'A', 'b' => 'B', 'A-' => '!'))); }
/** * Test string literals in the context of if and unless helpers * * @param string $template template text * @param array $data context data * @param string $results The Expected Results * * @dataProvider stringLiteralsInIfAndUnlessHelpersProvider * * @return void */ public function testStringLiteralsInIfAndUnlessHelpers($template, $data, $results) { $engine = new \Handlebars\Handlebars(); $engine->addHelper('add', function ($template, $context, $args) { $sum = 0; foreach ($template->parseArguments($args) as $value) { $sum += intval($context->get($value)); } return $sum; }); $res = $engine->render($template, $data); $this->assertEquals($res, $results); }
/** * Custom helper test */ public function testCustomHelper() { $loader = new \Handlebars\Loader\StringLoader(); $engine = new \Handlebars\Handlebars(array('loader' => $loader)); $engine->addHelper('test', function () { return 'Test helper is called'; }); $this->assertEquals('Test helper is called', $engine->render('{{#test}}', array())); $this->assertEquals('Test helper is called', $engine->render('{{test}}', array())); $engine->addHelper('test2', function ($template, $context, $arg) { return 'Test helper is called with ' . $arg; }); $this->assertEquals('Test helper is called with a b c', $engine->render('{{#test2 a b c}}', array())); $this->assertEquals('Test helper is called with a b c', $engine->render('{{test2 a b c}}', array())); $engine->addHelper('renderme', function () { return new \Handlebars\String("{{test}}"); }); $this->assertEquals('Test helper is called', $engine->render('{{#renderme}}', array())); $engine->addHelper('dontrenderme', function () { return "{{test}}"; }); $this->assertEquals('{{test}}', $engine->render('{{#dontrenderme}}', array())); }