Skip to content

Commit 77563c8

Browse files
committed
Fix implementation to not render helper output
This ensures compatibility with the official Handlebars implementation. See #26
1 parent a16204e commit 77563c8

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ HandleBars change log
33

44
## ?.?.? / ????-??-??
55

6+
## 8.1.0 / 2023-04-07
7+
8+
* Fixed implementation to not render helper output. This ensures
9+
compatibility with the official Handlebars implementation. See
10+
issue #26
11+
(@thekid)
612
* Merged PR #25: Migrate to new testing library - @thekid
713

814
## 8.0.0 / 2022-10-08

src/main/php/com/handlebarsjs/DefaultContext.class.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ public final function newInstance($result, Context $parent= null) {
3636
return new self($result, $parent ?: $this);
3737
}
3838

39+
/**
40+
* Returns a rendering of a given helper closure
41+
*
42+
* @see https://github.com/xp-forge/handlebars/issues/26
43+
* @param var $closure
44+
* @param com.github.mustache.Node $node
45+
* @param string[] $options
46+
* @param string $start
47+
* @param string $end
48+
* @return string
49+
*/
50+
public function asRendering($closure, $node, $options= [], $start= '{{', $end= '}}') {
51+
$pass= [];
52+
foreach ($options as $key => $option) {
53+
$pass[$key]= $this->isCallable($option) ? $option($node, $this, []) : $option;
54+
}
55+
return $closure($node, $this, $pass);
56+
}
57+
3958
/**
4059
* Parse input into segments. Handles literal segments including quoted
4160
* strings, e.g. `input.["item-class"]`.

src/test/php/com/handlebarsjs/unittest/ExecutionTest.class.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ protected function evaluate($template, $variables, $templates= ['test' => 'Parti
1818
->withTemplates(new InMemory($templates))
1919
->withHelper('date', function($node, $context, $options) { return date('Y-m-d', $options[0] ?? time()); })
2020
->withHelper('time', ['short' => ['24' => function($node, $context, $options) { return date('H:i', $options[0] ?? time()); }]])
21+
->withHelper('code', function($node, $context, $options) { return '<code>'.$options[0].'</code>'; })
2122
->render($template, $variables)
2223
;
2324
}
@@ -203,4 +204,14 @@ public function undefined_partial() {
203204
public function undefined_dynamic_partial() {
204205
$this->evaluate('{{> (template)}}', ['template' => 'undefined']);
205206
}
207+
208+
#[Test]
209+
public function handlebars_in_replacement() {
210+
Assert::equals('{{name}}', $this->evaluate('{{name}}', ['name' => '{{name}}']));
211+
}
212+
213+
#[Test]
214+
public function handlebars_returned_from_helper() {
215+
Assert::equals('<code>{{name}}</code>', $this->evaluate('{{& code in}}', ['in' => '{{name}}']));
216+
}
206217
}

0 commit comments

Comments
 (0)