Skip to content

Commit

Permalink
Fix implementation to not render helper output
Browse files Browse the repository at this point in the history
This ensures compatibility with the official Handlebars implementation.
See #26
  • Loading branch information
thekid committed Apr 7, 2023
1 parent a16204e commit 77563c8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ HandleBars change log

## ?.?.? / ????-??-??

## 8.1.0 / 2023-04-07

* Fixed implementation to not render helper output. This ensures
compatibility with the official Handlebars implementation. See
issue #26
(@thekid)
* Merged PR #25: Migrate to new testing library - @thekid

## 8.0.0 / 2022-10-08
Expand Down
19 changes: 19 additions & 0 deletions src/main/php/com/handlebarsjs/DefaultContext.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ public final function newInstance($result, Context $parent= null) {
return new self($result, $parent ?: $this);
}

/**
* Returns a rendering of a given helper closure
*
* @see https://github.com/xp-forge/handlebars/issues/26
* @param var $closure
* @param com.github.mustache.Node $node
* @param string[] $options
* @param string $start
* @param string $end
* @return string
*/
public function asRendering($closure, $node, $options= [], $start= '{{', $end= '}}') {
$pass= [];
foreach ($options as $key => $option) {
$pass[$key]= $this->isCallable($option) ? $option($node, $this, []) : $option;
}
return $closure($node, $this, $pass);
}

/**
* Parse input into segments. Handles literal segments including quoted
* strings, e.g. `input.["item-class"]`.
Expand Down
11 changes: 11 additions & 0 deletions src/test/php/com/handlebarsjs/unittest/ExecutionTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected function evaluate($template, $variables, $templates= ['test' => 'Parti
->withTemplates(new InMemory($templates))
->withHelper('date', function($node, $context, $options) { return date('Y-m-d', $options[0] ?? time()); })
->withHelper('time', ['short' => ['24' => function($node, $context, $options) { return date('H:i', $options[0] ?? time()); }]])
->withHelper('code', function($node, $context, $options) { return '<code>'.$options[0].'</code>'; })
->render($template, $variables)
;
}
Expand Down Expand Up @@ -203,4 +204,14 @@ public function undefined_partial() {
public function undefined_dynamic_partial() {
$this->evaluate('{{> (template)}}', ['template' => 'undefined']);
}

#[Test]
public function handlebars_in_replacement() {
Assert::equals('{{name}}', $this->evaluate('{{name}}', ['name' => '{{name}}']));
}

#[Test]
public function handlebars_returned_from_helper() {
Assert::equals('<code>{{name}}</code>', $this->evaluate('{{& code in}}', ['in' => '{{name}}']));
}
}

0 comments on commit 77563c8

Please sign in to comment.