if
You can use the if helper to conditionally render a block. If its argument returns false
, undefined
, null
, ""
, 0
, or []
, IdealDoc will not render the block.
{{#if author}} {{firstName}} {{lastName}} {{/if}}
When you pass the following input to the above template
{ author: true, firstName: "Yehuda", lastName: "Katz", }
This will produce the result as below:
Yehuda Katz
If the input is an empty JSONObject {}
, then author will become undefined and if condition fails, resulting in an empty output
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".
{{#if author}} {{firstName}} {{lastName}} {{else}} Unknown Author {{/if}}
with
The with-helper allows you to change the evaluation context of template-part.
{{#with person}} {{firstname}} {{lastname}} {{/with}}
when used with this context:
{ person: { firstname: "Yehuda", lastname: "Katz", }, }
will result in:
Yehuda Katz
with can also be used with block parameters to define known references in the current block. The example above can be converted to:
{{#with city as | city |}} {{#with city.location as | loc |}} {{city.name}}: {{loc.north}} {{loc.east}} {{/with}} {{/with}}
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.
{{#with city}} {{city.name}} (not shown because there is no city) {{else}} No city found {{/with}}
{ person: { firstname: "Yehuda", lastname: "Katz", }, }
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.
{{#if 0 includeZero=true}} Does render {{/if}}
Conditionals may also be chained by including the subsequent helper call within the else directive.
{{#if isActive}} show if isActive is true {{else if isInactive}} show if isInactive true {{/if}}
It is not necessary to use the same helper in subsequent calls, the unless helper could be used in the else portion as with any other helper. When the helper values are different, the closing directive should match the opening helper name.
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.
{{#unless license}} WARNING: This entry does not have a license! {{/unless}}
If looking up license under the current context returns a falsy value, IdealDoc will render the warning. Otherwise, it will render nothing.
## compare
Syntax{{compare param1 operator param2}}
param1 and param2 are the parameters to be compared. They can be either static string values enclosed in quotes (e.g. compare “2” “>” “1”) or dynamic variables (e.g. a “>” b).
The operator is a string argument enclosed in quotes defining one of the following operators to be applied:
equals:
==
or===
not equals:
!=
or!==
less than:
<
less or equal:
<=
greater than:
>
greater or equal:
>=
compare can be used as block or inline expression.
Block Expression:
{{#compare a ">" b}} shown if a greater than b {{else}} (optional) if an invers (else) expression is provided {{/compare}}
As single expression:
{{#if (compare a ">" b)}} shown if a greater than b {{else}} (optional) if an invers (else) expression is provided {{/if}}
contains
Block helper that renders the block if collection has the given value, using strict equality (===
) for comparison, otherwise the inverse block is rendered (if specified). If a startIndex is specified and is negative, it is used as the offset from the end of the collection.
Example input:
{ myArray: ['a', 'b', 'c'] }
Example template:
{{#contains myArray "c" startIndex=-1}} This will not be rendered. {{else}} This will be rendered. {{/contains}}
concat
Helper which concatenates several strings into one.
Example input:
{ myString: 'Hello' }
Example template:
{{concat myString " " "World!"}} --> results in "Hello World!"
## empty
Inline helper to evaluate whether a given String or an Array or an Object is empty or null/undefined
.
Example input:
{ emptyArray: [], emptyString: '', emptyObject: {} }
Example template:
{{#if (empty emptyArray)}} Array is empty. {{else}} Array is not empty. {{/contains}}