From 4bf343d122f39682cc4382f157bdcafaca4bd3c7 Mon Sep 17 00:00:00 2001 From: mai7star Date: Mon, 26 Jan 2015 13:54:22 +0900 Subject: [PATCH 1/2] add InlineLoader Class (import from Mustache PHP) --- src/Handlebars/Loader/InlineLoader.php | 100 +++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/Handlebars/Loader/InlineLoader.php diff --git a/src/Handlebars/Loader/InlineLoader.php b/src/Handlebars/Loader/InlineLoader.php new file mode 100644 index 0000000..edeed2e --- /dev/null +++ b/src/Handlebars/Loader/InlineLoader.php @@ -0,0 +1,100 @@ +load('hello'); + * $goodbye = $loader->load('goodbye'); + * + * __halt_compiler(); + * + * @@ hello + * Hello, {{ planet }}! + * + * @@ goodbye + * Goodbye, cruel {{ planet }} + * + * Templates are deliniated by lines containing only `@@ name`. + */ + +namespace Handlebars\Loader; + +use Handlebars\Loader; +use Handlebars\String; + +class InlineLoader implements Loader +{ + protected $_fileName; + protected $_offset; + protected $_templates; + + /** + * The InlineLoader requires a filename and offset to process templates. + * The magic constants `__FILE__` and `__COMPILER_HALT_OFFSET__` are usually + * perfectly suited to the job: + * + * $loader = new \Handlebars\Loader\InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__); + * + * Note that this only works if the loader is instantiated inside the same + * file as the inline templates. If the templates are located in another + * file, it would be necessary to manually specify the filename and offset. + * + * @param string $fileName The file to parse for inline templates + * @param int $offset A string offset for the start of the templates. + * This usually coincides with the `__halt_compiler` + * call, and the `__COMPILER_HALT_OFFSET__`. + */ + public function __construct($fileName, $offset) + { + if (!is_file($fileName)) { + throw new \InvalidArgumentException('InlineLoader expects a valid filename.'); + } + + if (!is_int($offset) || $offset < 0) { + throw new \InvalidArgumentException('InlineLoader expects a valid file offset.'); + } + + $this->_fileName = $fileName; + $this->_offset = $offset; + } + + /** + * Load a Template by name. + * + * @param string $name + * + * @return string Mustache Template source + */ + public function load($name) + { + $this->loadTemplates(); + + if (!array_key_exists($name, $this->_templates)) { + throw new \InvalidArgumentException("Template {$name} not found."); + } + + return $this->_templates[$name]; + } + + /** + * Parse and load templates from the end of a source file. + */ + protected function loadTemplates() + { + if (!is_null($this->_templates)) { + return; + } + + $this->_templates = array(); + $data = file_get_contents($this->_fileName, false, null, $this->_offset); + foreach (preg_split('/^@@(?= [\w\d\.]+$)/m', $data, -1) as $chunk) { + if (trim($chunk)) { + list($name, $content) = explode("\n", $chunk, 2); + $this->_templates[trim($name)] = trim($content); + } + } + } +} \ No newline at end of file From 317e1ebd6c768c9c28bb206e8eb7030180fa1e55 Mon Sep 17 00:00:00 2001 From: Hiroyuki Toda Date: Mon, 26 Jan 2015 21:37:51 +0900 Subject: [PATCH 2/2] add test for InlineLoader --- tests/Xamin/HandlebarsTest.php | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index 9fa5d5b..283b8a3 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -619,6 +619,23 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase $this->assertEquals('test', $engine->render('loader', array())); } + /** + * Test inline loader + */ + public function testInlineLoader() + { + $loader = new \Handlebars\Loader\InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__); + $this->assertEquals('This is a inline template.', $loader->load('template1')); + + $expected = <<assertEquals($expected, $loader->load('template2')); + } + /** * Test file system loader */ @@ -1141,7 +1158,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase { $loader = new \Handlebars\Loader\StringLoader(); $engine = new \Handlebars\Handlebars(array('loader' => $loader)); - + $this->assertEquals('good', $engine->render('{{#with b}}{{#if this}}{{../../a}}{{/if}}{{/with}}', array('a' => 'good', 'b' => 'stump'))); $this->assertEquals('good', $engine->render('{{#with b}}{{#unless false}}{{../../a}}{{/unless}}{{/with}}', array('a' => 'good', 'b' => 'stump'))); } @@ -1156,3 +1173,17 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase $this->assertEquals($argsString, (string)$args); } } + +/** + * Testcase for testInlineLoader + * + */ +__halt_compiler(); +@@ template1 +This is a inline template. + +@@ template2 +a +b +c +d \ No newline at end of file