diff --git a/src/Handlebars/Loader/InlineLoader.php b/src/Handlebars/Loader/InlineLoader.php index edeed2e..5cc2908 100644 --- a/src/Handlebars/Loader/InlineLoader.php +++ b/src/Handlebars/Loader/InlineLoader.php @@ -1,5 +1,8 @@ + * @author Hiroyuki Toda + * @copyright 2010-2015 (c) Justin Hileman + * @copyright 2015 (c) fzerorubigd + * @license MIT + * @version Release: @package_version@ + * @link http://xamin.ir */ namespace Handlebars\Loader; use Handlebars\Loader; -use Handlebars\String; +/** + * The inline loader + * + * @category Xamin + * @package Handlebars + * @author fzerorubigd + * @author Hiroyuki Toda + * @copyright 2010-2015 (c) Justin Hileman + * @copyright 2015 (c) fzerorubigd + * @license MIT + * @version Release: @package_version@ + * @link http://xamin.ir * + */ class InlineLoader implements Loader { - protected $_fileName; - protected $_offset; - protected $_templates; + protected $fileName; + protected $offset; + protected $templates; /** * The InlineLoader requires a filename and offset to process templates. @@ -57,43 +82,45 @@ class InlineLoader implements Loader throw new \InvalidArgumentException('InlineLoader expects a valid file offset.'); } - $this->_fileName = $fileName; - $this->_offset = $offset; + $this->fileName = $fileName; + $this->offset = $offset; } /** * Load a Template by name. * - * @param string $name + * @param string $name template name * - * @return string Mustache Template source + * @return string Handlebars Template source */ public function load($name) { $this->loadTemplates(); - if (!array_key_exists($name, $this->_templates)) { + if (!array_key_exists($name, $this->templates)) { throw new \InvalidArgumentException("Template {$name} not found."); } - return $this->_templates[$name]; + return $this->templates[$name]; } /** * Parse and load templates from the end of a source file. + * + * @return void */ protected function loadTemplates() { - if (!is_null($this->_templates)) { + if (!is_null($this->templates)) { return; } - $this->_templates = array(); - $data = file_get_contents($this->_fileName, false, null, $this->_offset); + $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); + list($name, $content) = explode("\n", $chunk, 2); + $this->templates[trim($name)] = trim($content); } } } diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index d8527c0..a60ae3d 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -16,7 +16,7 @@ */ /** - * Class AutoloaderTest + * Class HandlebarsTest */ class HandlebarsTest extends \PHPUnit_Framework_TestCase { @@ -339,7 +339,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase array('data' => array(1, 2, 3, 4)), '1234' ), - array( '{{#if first}}The first{{else}}{{#if second}}The second{{/if}}{{/if}}', + array('{{#if first}}The first{{else}}{{#if second}}The second{{/if}}{{/if}}', array('first' => false, 'second' => true), 'The second' ) @@ -352,7 +352,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase public function testHelpersManagement() { $helpers = new \Handlebars\Helpers(array('test' => function () { - }), false); + }), false); $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); $this->assertTrue(is_callable($engine->getHelper('test'))); $this->assertTrue($engine->hasHelper('test')); @@ -509,11 +509,14 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase $this->assertFalse($helpers->has('test')); $this->assertFalse(isset($helpers->test)); $this->assertTrue($helpers->isEmpty()); - $helpers->add('test', function() {}); + $helpers->add('test', function () { + }); $this->assertCount(0, array_diff(array_keys($helpers->getAll()), array('test'))); $extraHelpers = new \Handlebars\Helpers(); - $extraHelpers->add('test', function() {}); - $extraHelpers->add('test2', function() {}); + $extraHelpers->add('test', function () { + }); + $extraHelpers->add('test2', function () { + }); $helpers->addHelpers($extraHelpers); $this->assertTrue($helpers->has('test2')); $this->assertEquals($helpers->test, $extraHelpers->test); @@ -1135,10 +1138,10 @@ EOM; $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) { + $engine->addHelper('concat', function (\Handlebars\Template $template, \Handlebars\Context $context, $args) { $result = ''; - foreach($template->parseArguments($args) as $arg) { + foreach ($template->parseArguments($args) as $arg) { $result .= $context->get($arg); } @@ -1172,28 +1175,29 @@ EOM; $args = new \Handlebars\Arguments($argsString); $this->assertEquals($argsString, (string)$args); } - - - public function stringLiteralsInIfAndUnlessHelpersProvider() { - return array( + + + public function stringLiteralsInIfAndUnlessHelpersProvider() + { + return array( // IfHelper - array('{{#if "truthyString"}}true{{else}}false{{/if}}', array(), "true"), + array('{{#if "truthyString"}}true{{else}}false{{/if}}', array(), "true"), array("{{#if 'truthyStringSingleQuotes'}}true{{else}}false{{/if}}", array(), "true"), - array("{{#if ''}}true{{else}}false{{/if}}", array(), "false"), + array("{{#if ''}}true{{else}}false{{/if}}", array(), "false"), array("{{#if '0'}}true{{else}}false{{/if}}", array(), "false"), array("{{#if (add 0 1)}}true{{else}}false{{/if}}", array(), "true"), array("{{#if (add 1 -1)}}true{{else}}false{{/if}}", array(), "false"), - // UnlessHelper - array('{{#unless "truthyString"}}true{{else}}false{{/unless}}', array(), "false"), + // UnlessHelper + array('{{#unless "truthyString"}}true{{else}}false{{/unless}}', array(), "false"), array("{{#unless 'truthyStringSingleQuotes'}}true{{else}}false{{/unless}}", array(), "false"), - array("{{#unless ''}}true{{else}}false{{/unless}}", array(), "true"), + array("{{#unless ''}}true{{else}}false{{/unless}}", array(), "true"), array("{{#unless '0'}}true{{else}}false{{/unless}}", array(), "true"), array("{{#unless (add 0 1)}}true{{else}}false{{/unless}}", array(), "false"), array("{{#unless (add 1 -1)}}true{{else}}false{{/unless}}", array(), "true"), ); } - - /** + + /** * Test string literals in the context of if and unless helpers * * @param string $template template text @@ -1207,21 +1211,21 @@ EOM; 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; + $sum = 0; + + foreach ($template->parseArguments($args) as $value) { + $sum += intval($context->get($value)); + } + + return $sum; }); - + $res = $engine->render($template, $data); $this->assertEquals($res, $results); } - + } /**