Skip to content

Commit 53db6df

Browse files
committed
Expand helper options documentation in readme
1 parent b7127eb commit 53db6df

1 file changed

Lines changed: 43 additions & 14 deletions

File tree

README.md

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ PHP Handlebars compiles and executes complex templates up to 40% faster than Lig
1010
| Library | Compile time | Runtime | Total time | Peak memory usage |
1111
|--------------------|--------------|---------|------------|-------------------|
1212
| LightnCandy 1.2.6 | 5.2 ms | 2.8 ms | 8.0 ms | 5.3 MB |
13-
| PHP Handlebars 1.0 | 3.5 ms | 1.6 ms | 5.1 ms | 3.6 MB |
13+
| PHP Handlebars 1.1 | 3.5 ms | 1.6 ms | 5.1 ms | 3.6 MB |
1414

1515
_Tested on PHP 8.5 with the JIT enabled. See the `benchmark` branch to run the same test._
1616

@@ -72,39 +72,37 @@ echo $template(['first' => 'John']); // Error: "last" not defined
7272

7373
* `knownHelpers`: Associative array (`helperName => bool`) of helpers known to exist at template execution time.
7474
Passing this allows the compiler to optimize a number of cases.
75-
Builtin helpers are automatically included in this list and may be omitted by setting that value to `false`.
76-
* `knownHelpersOnly`: Enable to allow further optimizations based on the known helpers list.
77-
* `noEscape`: Enable to not HTML escape any content.
75+
Built-in helpers are automatically included in this list and may be omitted by setting that value to `false`.
76+
* `knownHelpersOnly`: Set to `true` to allow further optimizations based on the known helpers list.
77+
* `noEscape`: Set to `true` to disable HTML escaping of output.
7878
* `strict`: Run in strict mode. In this mode, templates will throw rather than silently ignore missing fields.
7979
This has the side effect of disabling inverse operations such as `{{^foo}}{{/foo}}`
8080
unless fields are explicitly included in the source object.
8181
* `assumeObjects`: Removes object existence checks when traversing paths.
8282
This is a subset of strict mode that generates optimized templates when the data inputs are known to be safe.
83-
* `preventIndent`: Prevent indented partial-call from indenting the entire partial output by the same amount.
83+
* `preventIndent`: Prevents an indented partial call from indenting the entire partial output by the same amount.
8484
* `ignoreStandalone`: Disables standalone tag removal.
8585
When set, blocks and partials that are on their own line will not remove the whitespace on that line.
8686
* `explicitPartialContext`: Disables implicit context for partials.
8787
When enabled, partials that are not passed a context value will execute against an empty object.
88-
* `partials`: Provide a `name => value` array of custom partial template strings.
89-
* `partialResolver`: A closure which will be called for any partial not in the `partials` array to return a template for it.
88+
* `partials`: An associative array of custom partial template strings (`name => template`).
89+
* `partialResolver`: A closure that will be called for any partial not found in the `partials` array,
90+
and should return a template string for it.
9091

9192
## Runtime Options
9293

9394
`Handlebars::compile` returns a closure which can be invoked as `$template($context, $options)`.
9495
The `$options` parameter takes an array of runtime options, accepting the following keys:
9596

96-
* `data`: An array to define custom `@variable` private variables.
97-
* `helpers`: An `array<string, \Closure>` containing custom helpers to add to the built-in helpers.
98-
* `partials`: An `array<string, \Closure>` containing partial functions precompiled with `Handlebars::compile`.
99-
This is useful if multiple templates sharing the same partials need to be compiled and rendered, and you don't want
100-
to recompile the same partials over and over for each template.
97+
* `data`: An associative array of initial `@data` variables (e.g. `['version' => '1.0']` makes `@version` available in the template).
98+
* `helpers`: An `array<string, \Closure>` of helpers to merge with the built-in helpers. Can also be used to override a built-in helper by using the same name.
99+
* `partials`: An `array<string, \Closure>` of partial closures precompiled with `Handlebars::compile`.
100+
Useful when multiple templates share the same partials, and you want to avoid recompiling them for each template.
101101

102102
## Custom Helpers
103103

104104
Helper functions will be passed any arguments provided to the helper in the template.
105105
If needed, a final `$options` parameter can be included which will be passed a `HelperOptions` instance.
106-
This object contains properties for accessing `hash` arguments, `data`, and the current `scope`, `name`,
107-
as well as `fn()` and `inverse()` methods to render the block and else contents, respectively.
108106

109107
For example, a custom `#equals` helper with JS equality semantics could be implemented as follows:
110108

@@ -131,6 +129,37 @@ echo $template(['my_var' => 1], $options); // Not equal
131129
echo $template(['my_var' => null], $options); // Not equal
132130
```
133131

132+
### HelperOptions Reference
133+
134+
**Properties**
135+
136+
* `name` (readonly `string`): The helper name as it appeared in the template.
137+
Useful in `helperMissing`/`blockHelperMissing` hooks to identify which name was called.
138+
* `hash` (readonly `array`): Key/value pairs passed as hash arguments in the template
139+
(e.g. `{{helper foo=1 bar="x"}}` produces `['foo' => 1, 'bar' => 'x']`).
140+
* `blockParams` (readonly `int`): The number of block parameters declared by the helper call
141+
(e.g. `{{#helper as |a b|}}` produces `2`).
142+
* `scope` (`mixed`): The current evaluation context (equivalent to `this` in a Handlebars.js helper).
143+
Can be reassigned inside a helper to change the context passed to `fn()`.
144+
* `data` (`array`): The current `@data` frame. Contains `@`-prefixed private variables such as
145+
`root`, `index`, `key`, `first`, `last`, and `_parent`. Can be read or modified inside a helper.
146+
147+
**Methods**
148+
149+
* `fn(mixed $context = <current scope>, mixed $data = null): string`: Renders the block body.
150+
Pass a new context as `$context` to change what the block renders against (equivalent to `options.fn(newContext)` in JS).
151+
Pass a `$data` array with a `'data'` key to inject additional `@`-prefixed variables into the block,
152+
and/or a `'blockParams'` key containing an array of values to expose as block parameters.
153+
* `inverse(mixed $context = null, mixed $data = null): string`: Renders the `{{else}}` / inverse block.
154+
Returns an empty string if no inverse block was provided.
155+
Accepts the same optional `$context` and `$data` arguments as `fn()`.
156+
* `hasPartial(string $name): bool`: Returns `true` if a partial with the given name is registered in the
157+
current render context. Useful alongside `registerPartial()` to implement lazy partial loading.
158+
* `registerPartial(string $name, \Closure $partial): void`: Registers a compiled partial closure under
159+
the given name for the current render context. The closure must be produced by `Handlebars::compile`.
160+
161+
`isset($options->fn)` and `isset($options->inverse)` can be used to test whether a block or inverse block was provided.
162+
134163
## Hooks
135164

136165
If a custom helper named `helperMissing` is defined, it will be called when a mustache or a block-statement

0 commit comments

Comments
 (0)