Skip to content

Commit

Permalink
Fix partial blocks within each
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Sep 17, 2017
1 parent 8d23ec6 commit 29517dd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main/php/com/handlebarsjs/PartialBlockHelper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,20 @@ public function __construct($options= [], NodeList $fn= null, NodeList $inverse=
*/
public function evaluate($context) {
$templates= $context->engine->getTemplates();
$block= $this->fn->evaluate($context);

// Eagerly evaluate inline partials
$block= new NodeList();
foreach ($this->fn as $node) {
if ($node instanceof InlinePartialHelper) {
$node->evaluate($context);
} else {
$block->add($node);
}
}

$source= $templates->source($this->name);
if ($source->exists()) {
$previous= $templates->register('@partial-block', $block);
$previous= $templates->register('@partial-block', $this->fn);

// {{#> partial context}} vs {{> partial key="Value"}}
if (isset($this->options[0])) {
Expand All @@ -51,7 +60,7 @@ public function evaluate($context) {
$templates->register('@partial-block', $previous);
}
} else {
return $block;
return $block->evaluate($context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public function nested_partial_block() {
$this->assertEquals('Inner Outer', $this->evaluate('{{#> layout}}Outer{{/layout}}', []));
}

#[@test]
public function partial_inside_each() {
$this->templates->add('list', '[{{#each .}}<item>{{> @partial-block}}</item>{{/each}}]');
$this->assertEquals(
'[<item>value = a</item><item>value = b</item><item>value = c</item>]',
$this->evaluate('{{#> list value}}value = {{.}}{{/list}}', ['value' => ['a', 'b', 'c']])
);
}

#[@test]
public function layout() {
$this->templates->add('includes/hero', '<div class="hero"><img src="{{hero-src}}" alt="{{hero-alt}}"/></div>');
Expand Down

0 comments on commit 29517dd

Please sign in to comment.