Articles in this section

#with helper

The #with block helper sets the context to a nested object for all expressions inside the block. Use it to access multiple fields within a nested object without repeating the parent path in each expression.

Syntax

{{#with object}} {{field1}} {{field2}} {{/with}}

object -- The nested object to use as the context inside the block. Required. All field references inside the block resolve relative to this object.

Examples

Access fields in a nested object

{{#with library}}{{title}} on "{{album}}" by {{artist}}{{/with}}

Input:

{
  "library": {
    "album": "The Sound",
    "title": "Danube Incident",
    "artist": "Lalo Schifrin"
  }
}

Output: Danube Incident on "The Sound" by Lalo Schifrin

Without #with, each reference would require the full path: {{library.title}}, {{library.album}}, {{library.artist}}.

Mix outer and inner context

The author{{#with author}} {{firstName}} {{lastName}}{{/with}}

Input:

{
  "title": "A Tale of Two Cities",
  "author": {
    "firstName": "Charles",
    "lastName": "Dickens"
  }
}

Output: The author Charles Dickens

Text and expressions outside the #with block resolve against the root context. Only expressions inside the block use the author object as their context.

Usage notes

  • Field references inside a #with block resolve relative to the specified object, not the root context.
  • To reference a root-level field from inside a #with block, use the ../ path prefix: {{../title}}.
  • If the specified object is missing or undefined, the block is skipped entirely and nothing renders.
  • #with does not iterate over arrays. To loop through array elements, use the #each helper instead.