Ejemplo n.º 1
0
 public function testReturnedSafeString()
 {
     $testinglinkHelper = $this->runtime->getHelper("testinglink");
     $result = $testinglinkHelper("https://github.com", "GitHub");
     $this->assertInstanceOf(Handlebars\SafeString::class, $result);
     $this->assertEquals('<a href="https://github.com">GitHub</a>', (string) $result);
 }
Ejemplo n.º 2
0
 protected function setUp()
 {
     $runtime = new Handlebars\Runtime();
     $runtime->addHelper("startswithfoo", function ($value) {
         return "foo{$value}";
     });
     $runtime->addHelper("testinglink", function ($url, $text) {
         $string = sprintf('<a href="%1$s">%2$s</a>', $url, $text);
         return new Handlebars\SafeString($string);
     });
     $argumentParserFactory = new Handlebars\Argument\ArgumentParserFactory(new Handlebars\Argument\ArgumentListFactory());
     $this->compiler = new Handlebars\Compiler($runtime, new Handlebars\TokenizerFactory(), $argumentParserFactory);
 }
Ejemplo n.º 3
0
 /**
  * @param array $node
  * @return string
  * @throws Exception
  */
 protected function generatePartial(array $node) : string
 {
     $argumentList = $this->parseArguments($node['value']);
     $args = $argumentList->getArguments();
     $partialNameArg = array_shift($args);
     $partial = $this->runtime->getPartial($partialNameArg->getRawValue());
     if (is_null($partial) === true) {
         if ($partialNameArg instanceof StringArgument) {
             $partial = $partialNameArg->getRawValue();
         } else {
             // TODO: Add message
             throw new Exception();
         }
     }
     $compiler = new static($this->runtime, $this->tokenizerFactory, $this->argumentParserFactory);
     $code = $compiler->compile($partial, $this->offset + 3);
     // If there is at least one other argument, use it
     // as the scope for the partial.
     if (count($args)) {
         $scope = '\\r\\t\\1' . $args[0]->getValue() . ', ';
     } else {
         $scope = '';
     }
     $hash = array();
     foreach ($argumentList->getNamedArguments() as $key => $value) {
         $hash[$key] = sprintf('\'%s\' => %s', $key, $value->getValue());
     }
     if (empty($hash)) {
         $hash = '';
     } else {
         $hash = '\\r\\t\\1\\1\\1' . implode(', \\r\\t\\1\\1\\1', $hash) . '\\r\\t\\1\\1';
     }
     $layout = str_replace(array('\\r', '\\t', '\\1'), array("\n", str_repeat('    ', $this->offset), str_repeat('    ', 1)), '\\r\\t$buffer .= $this->runtime->getHelper(\'noop\')(' . $scope . '\\r\\t\\1array(' . '\\r\\t\\1\\1\'name\' => \'noop\',' . '\\r\\t\\1\\1\'hash\' => array(' . $hash . '),' . '\\r\\t\\1\\1\'fn\' => function($context = null) use ($data) {' . '\\r\\t\\1\\1\\1if (is_array($context)) {' . '\\r\\t\\1\\1\\1\\1$data->push($context);' . '\\r\\t\\1\\1\\1}' . '\\r\\r\\t\\1\\1\\1$buffer = \'\';' . '%s' . '\\r\\t\\1\\1\\1if (is_array($context)) {' . '\\r\\t\\1\\1\\1\\1$data->pop();' . '\\r\\t\\1\\1\\1}' . '\\r\\r\\t\\1\\1\\1return $buffer;' . '\\r\\t\\1\\1},' . '\\r\\t\\1\\1\'inverse\' => function() {},' . '\\r\\t\\1)' . '\\r\\t);\\r');
     return sprintf($layout, $code);
 }
Ejemplo n.º 4
0
 public function testAddPartial3()
 {
     $runtime = new Handlebars\Runtime();
     $runtime->addPartial("foo", "This is {{ foo }}");
     $runtime->addPartial("bar", "Foo is not {{ something }}");
     $handlebars = new Handlebars\Handlebars($runtime, call_user_func($this->compilerFactory, $runtime), $this->dataFactory);
     $template = $handlebars->compile("{{> foo }} ... {{> bar zoo something='Amazing'}}");
     $result = $template->render(array("foo" => "FOO", "bar" => "BAR", "zoo" => array("bar" => "ZOO")));
     $this->assertEquals("This is FOO ... Foo is not Amazing", $result);
 }