Skip to main content

Block helpers

Bigodin ships only the block primitives wired to template syntax. String, math, date, array, and comparison helpers were removed in 3.0.0; register your own with bigodin.addHelper(name, fn). See Library API for addHelper and the tutorial for a worked example.

HelperPurpose
ifRun a block when the value is truthy. Does not change context.
unlessRun a block when the value is falsy. Does not change context.
withRun a block once with the value as the new context.
eachIterate a block over an array (or once over a non-array).
returnHalt the execution; the template returns what has been rendered so far.

if

{{#if user.active}}
Welcome,
{{user.name}}!
{{else}}
Account inactive.
{{/if}}

Coerces the argument to a boolean. The body runs when truthy; an optional {{else}} branch runs when falsy. Context is not changed inside the block.

if chains via {{else if ...}}:

{{#if (eq country 'BR')}}
Brazil
{{else if (eq country 'US')}}
United States
{{else}}
Other
{{/if}}

unless

{{#unless user.banned}}
Hello,
{{user.name}}.
{{else}}
Access denied.
{{/unless}}

Inverse of if. The body runs when the argument is falsy.

with

{{#with user.address}}
{{street}},
{{city}}
{{/with}}

Pushes the argument as the current context for the duration of the block. Inside, {{$this}} and bare paths resolve against the new context. The block runs exactly once. Falsy values render the {{else}} branch (if any), otherwise nothing.

Accepts block params: {{#with user as |u|}} binds the pushed context to a local name; with multiple arguments, each name binds to the corresponding frame ({{#with a b as |x y|}}).

Multiple arguments

with accepts more than one argument and pushes each truthy argument onto the context stack as its own frame. The body still runs exactly once. Inside the body, bare paths resolve against the innermost frame (the rightmost argument); reach the outer frames with $parent, $parent.$parent, and so on, or with $root for the original context.

{{#with user company}}
{{name}}
works at
{{$parent.name}}
{{/with}}

over { user: { name: 'Alice' }, company: { name: 'Acme' } } renders Acme works at Alice (the rightmost company is on top, so bare name resolves to Acme; $parent.name walks down one frame to user).

Falsy arguments are skipped: {{#with maybe user}}...{{/with}} over { maybe: null, user: {...} } pushes only user. If every argument is falsy, the block falls through to its {{else}} branch (if any), otherwise renders nothing.

Negated form ({{^with ...}}) renders its body when every argument is falsy.

Overriding with

with is implemented natively at the block level. Registering your own helper named with via addHelper('with', fn) overrides the native behavior - the user-supplied function is called with the resolved arguments and its return value drives the block exactly like any other helper (truthy renders the body once, arrays iterate, etc.).

each

{{#each items}}
-
{{$this}}
{{/each}}

Iterates over an array, pushing each element as the current context. A non-array argument is treated as a single-element list (the block runs once with that value as context). Empty arrays render nothing.

Accepts block params: {{#each items as |item index|}} binds the current element and its index to local names.

return

Hello{{#if shouldStop}}{{return}}{{/if}}, world!

Halts the rest of the template. The runner returns whatever output has accumulated up to that point. The programmatic equivalent for a custom helper is this.halt(); see Library API.