# Built-in Helpers
# #if
You can use the if
helper to conditionally render a block. If its argument returns false
, undefined
, null
, ""
,
0
, or []
, Handlebars will not render the block.
When you pass the following input to the above template
This will produce the result as below:
If the input is an empty JSONObject {}
, then author
will become undefined
and if
condition fails, resulting in
the output as follow:
<div class="entry"></div>
When using a block expression, you can specify a template section to run if the expression returns a falsy value. The
section, marked by else
is called an "else section".
# includeZero
The includeZero=true
option may be set to treat the conditional as not empty.
This effectively determines if 0
is handled by the positive or negative path.
# Sub-Expressions
Helpers are the proposed way to add custom logic to templates. You can write any helper and use it in a sub-expression.
For example, in checking for initialization of a variable the built-in #if
check might not be appropriate as it
returns false for empty collections (see Utils.isEmpty).
You could write a helper that checks for "undefined" such as:
Then use your helper as a sub-expression:
# #unless
You can use the unless
helper as the inverse of the if
helper. Its block will be rendered if the expression returns
a falsy value.
If looking up license
under the current context returns a falsy value, Handlebars will render the warning. Otherwise,
it will render nothing.
# #each
You can iterate over a list using the built-in each
helper. Inside the block, you can use this
to reference the
element being iterated over.
when used with this context:
will result in:
You can use the this
expression in any context to reference the current context.
You can optionally provide an else
section which will display only when the list is empty.
When looping through items in each
, you can optionally reference the current loop index via {{@index}}
.
Additionally for object iteration, {{@key}}
references the current key name:
The first and last steps of iteration are noted via the @first
and
@last
variables when iterating over an array.
Nested each
blocks may access the iteration variables via depth based paths. To access the parent index, for example,
{{@../index}}
can be used.
# #with
The with
-helper allows you to change the evaluation context of template-part.
when used with this context:
will result in:
with
can also be used with block parameters to define known references in the current block. The example above can be
converted to
Which allows for complex templates to potentially provide clearer code than ../
depthed references allow for.
You can optionally provide an {{else}}
section which will display only when the passed value is empty.
# lookup
The lookup
helper allows for dynamic parameter resolution using Handlebars variables.
This is useful for resolving values for array indexes.
It can also be used to lookup properties of object based on data from the input. The following is a more complex example
that uses lookup
in a sub-expression to change the evaluation context to another object based on a property-value.
# log
The log
helper allows for logging of context state while executing a template.
It delegates to Handlebars.logger.log
which may be overridden to perform custom logging.
Any number of arguments may be passed to this method and all will be forwarded to the logger.
The log level may be set using the level hash parameter. Supported values are debug, info, warn, and error. When omitted, info is the default value,
Logging is conditional based on the level and to value set in Handlebars.logger.level
, which defaults to info
. All
log statements at or above the current level will be output.
← Block Helpers Hooks →