You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `helpers` compilation option has been removed, and all helpers must now be passed at runtime when executing the template (just like in Handlebars.js).
This can significantly reduce compile time, since PHP files no longer have to be read and tokenized to extract helper functions. It also enables sharing helper closures across multiple templates, and removes limitations on what they can access and do.
Additionally, implemented `knownHelpers` option and updated `knownHelpersOnly` to work the same as in Handlebars.js. This now makes it possible to disable individual built-in helpers. Fixed multiple bugs related to inverted block helpers and section scoping/iteration.
Also unified partial closures and improved param types. Compiled templates and partials now have the same function signature to avoid duplicated logic.
echo $template(['first' => 'John']); // Error: Runtime: last does not exist
59
59
```
60
60
61
-
**Available Options:**
61
+
### Available Options
62
+
63
+
*`knownHelpers`: Associative array (`helperName => bool`) of helpers known to exist at template execution time.
64
+
Passing this allows the compiler to optimize a number of cases.
65
+
Builtin helpers are automatically included in this list and may be omitted by setting that value to `false`.
62
66
*`knownHelpersOnly`: Enable to allow further optimizations based on the known helpers list.
63
67
*`noEscape`: Enable to not HTML escape any content.
64
68
*`strict`: Run in strict mode. In this mode, templates will throw rather than silently ignore missing fields.
65
-
*`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.
69
+
This has the side effect of disabling inverse operations such as `{{^foo}}{{/foo}}`
70
+
unless fields are explicitly included in the source object.
71
+
*`assumeObjects`: Removes object existence checks when traversing paths.
72
+
This is a subset of strict mode that generates optimized templates when the data inputs are known to be safe.
66
73
*`preventIndent`: Prevent indented partial-call from indenting the entire partial output by the same amount.
67
-
*`ignoreStandalone`: Disables standalone tag removal. When set, blocks and partials that are on their own line will not remove the whitespace on that line.
68
-
*`explicitPartialContext`: Disables implicit context for partials. When enabled, partials that are not passed a context value will execute against an empty object.
69
-
*`helpers`: Provide a key => value array of custom helper functions.
70
-
*`partials`: Provide a key => value array of custom partial templates.
74
+
*`ignoreStandalone`: Disables standalone tag removal.
75
+
When set, blocks and partials that are on their own line will not remove the whitespace on that line.
76
+
*`explicitPartialContext`: Disables implicit context for partials.
77
+
When enabled, partials that are not passed a context value will execute against an empty object.
78
+
*`partials`: Provide a `name => value` array of custom partial templates.
71
79
*`partialResolver`: A closure which will be called for any partial not in the `partials` array to return a template for it.
72
80
73
81
## Custom Helpers
@@ -80,28 +88,30 @@ This object contains properties for accessing `hash` arguments, `data`, and the
80
88
For example, a custom `#equals` helper with JS equality semantics could be implemented as follows:
81
89
82
90
```php
83
-
use DevTheorem\Handlebars\{Handlebars, HelperOptions, Options};
91
+
use DevTheorem\Handlebars\{Handlebars, HelperOptions};
84
92
85
-
$template = Handlebars::compile('{{#equals my_var false}}Equal to false{{else}}Not equal{{/equals}}', new Options(
86
-
helpers: [
93
+
$template = Handlebars::compile('{{#equals my_var false}}Equal to false{{else}}Not equal{{/equals}}');
94
+
$options = [
95
+
'helpers' => [
87
96
'equals' => function (mixed $a, mixed $b, HelperOptions $options) {
88
97
$jsEquals = function (mixed $a, mixed $b): bool {
89
-
if ($a === null || $b === null) {
90
-
// in JS, null is not equal to blank string or false or zero
@@ -147,7 +157,10 @@ Helpers may return a `DevTheorem\Handlebars\SafeString` instance to prevent esca
147
157
When constructing the string that will be marked as safe, any external content should be properly escaped
148
158
using the `Handlebars::escapeExpression()` method to avoid potential security concerns.
149
159
150
-
## Missing features
160
+
## Missing Features
161
+
162
+
All syntax and language features from Handlebars.js 4.7.8 should work the same in PHP Handlebars,
163
+
with the following exceptions:
151
164
152
-
All syntax from Handlebars.js 4.7.8 should work the same in this implementation, with the following exception:
153
-
*Decorators ([deprecated in Handlebars.js](https://github.com/handlebars-lang/handlebars.js/blob/master/docs/decorators-api.md)) have not been implemented.
165
+
* Custom Decorators have not been implemented, as they are [deprecated in Handlebars.js](https://github.com/handlebars-lang/handlebars.js/blob/master/docs/decorators-api.md).
166
+
*The `data` and `compat` compilation options have not been implemented.
0 commit comments