Skip to content

Commit 86b5501

Browse files
committed
Fix how knownHelpersOnly works
1 parent 2b17618 commit 86b5501

6 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ echo $template(['first' => 'John']); // Error: Runtime: last does not exist
6262
```
6363

6464
**Available Options:**
65+
* `knownHelpersOnly`: Enable to allow further optimizations based on the known helpers list.
6566
* `noEscape`: Enable to not HTML escape any content.
6667
* `strict`: Run in strict mode. In this mode, templates will throw rather than silently ignore missing fields.
6768
* `assumeObjects`: Removes object existence checks when traversing paths. This is a subset of strict mode that generates optimized templates when the data inputs are known to be safe.

‎src/Compiler.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,13 @@ private function MustacheStatement(MustacheStatement $mustache): string
503503
$call = self::getRuntimeFunc('hbch', "\$cx, 'helperMissing', $params, \$in, $escapedName");
504504
return "'." . self::getRuntimeFunc($fn, $call) . ".'";
505505
}
506-
throw new \Exception('Missing helper: "' . $helperName . '"');
506+
if (!$this->context->options->knownHelpersOnly) {
507+
$params = $this->compileParams($mustache->params, $mustache->hash);
508+
$escapedName = self::quote($helperName);
509+
$call = self::getRuntimeFunc('dynhbch', "\$cx, $escapedName, $params, \$in");
510+
return "'." . self::getRuntimeFunc($fn, $call) . ".'";
511+
}
512+
throw new \Exception("You specified knownHelpersOnly, but used the unknown helper $helperName");
507513
}
508514

509515
// Plain variable — if helperMissing is registered, route missing identifiers to it

‎src/Options.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* @param null|Closure(Context, string):(string|null) $partialResolver
1313
*/
1414
public function __construct(
15+
public bool $knownHelpersOnly = false,
1516
public bool $noEscape = false,
1617
public bool $strict = false,
1718
public bool $assumeObjects = false,

‎src/Runtime.php‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,47 @@ public static function in(RuntimeContext $cx, string $p, \Closure $code): void
370370
}
371371
}
372372

373+
/**
374+
* For helper calls not registered at compile time: checks runtime helpers, then context closures.
375+
*
376+
* @param array<array<mixed>> $vars variables for the helper
377+
* @param array<string,array<mixed>|string|int|\Closure> $_this current rendering context for the helper
378+
*/
379+
public static function dynhbch(RuntimeContext $cx, string $name, array $vars, mixed &$_this): mixed
380+
{
381+
if (isset($cx->helpers[$name])) {
382+
return static::hbch($cx, $name, $vars, $_this);
383+
}
384+
385+
$fn = $_this[$name] ?? null;
386+
if ($fn instanceof \Closure) {
387+
$options = new HelperOptions(
388+
name: $name,
389+
hash: $vars[1],
390+
fn: fn() => '',
391+
inverse: fn() => '',
392+
blockParams: 0,
393+
scope: $_this,
394+
data: $cx->data,
395+
);
396+
$args = $vars[0];
397+
$args[] = $options;
398+
try {
399+
return $fn(...$args);
400+
} catch (\Throwable $e) {
401+
throw new \Exception("Custom helper '$name' error: " . $e->getMessage());
402+
}
403+
}
404+
405+
throw new \Exception('Missing helper: "' . $name . '"');
406+
}
407+
373408
/**
374409
* For single custom helpers.
375410
*
376411
* @param string $ch the name of custom helper to be executed
377412
* @param array<array<mixed>> $vars variables for the helper
378-
* @param array<string,array<mixed>|string|int> $_this current rendering context for the helper
413+
* @param array<string,array<mixed>|string|int|\Closure> $_this current rendering context for the helper
379414
* @param string|null $logicalName when set, use as options.name instead of $ch
380415
*/
381416
public static function hbch(RuntimeContext $cx, string $ch, array $vars, mixed &$_this, ?string $logicalName = null): mixed

‎tests/ErrorTest.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ public static function renderErrorProvider(): array
109109
'data' => ['foo' => ['bar' => 'strlen']],
110110
'expected' => '"foo.bar" is not a block helper function',
111111
],
112+
[
113+
'template' => '{{typeof hello}}',
114+
'expected' => 'Missing helper: "typeof"',
115+
],
112116
];
113117
}
114118

@@ -131,7 +135,8 @@ public static function errorProvider(): array
131135
return [
132136
[
133137
'template' => '{{typeof hello}}',
134-
'expected' => 'Missing helper: "typeof"',
138+
'options' => new Options(knownHelpersOnly: true),
139+
'expected' => 'You specified knownHelpersOnly, but used the unknown helper typeof',
135140
],
136141
[
137142
'template' => '{{#with}}OK!{{/with}}',

‎tests/RegressionTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function testRuntimeHelpers(): void
5151
$result = $template($data);
5252
$this->assertSame('Hello runtime data', $result);
5353

54+
// runtime helpers option overrides functions in data
5455
$result = $template($data, [
5556
'helpers' => [
5657
'test' => fn(string $arg) => "$arg runtime option",

0 commit comments

Comments
 (0)