Skip to content

Conformance Examples 0.1.0 — Render: with-block

with-pushes-object-context

A {{#with}} block pushes a resolved object onto the context stack.

Template

{{#with user }}{{ name }}{{/with}}

Context

{
    "user": {
        "name": "Alice"
    }
}

Output

Alice

with-this-renders-pushed-context

{{this}} inside a with block renders the pushed context as JSON (unescaped, as it is not a string primitive).

Template

{{#with user }}{{ this }}{{/with}}

Context

{
    "user": {
        "name": "Alice"
    }
}

Output

{"name":"Alice"}

with-dot-shortcut

{{ . }} inside a with block renders the pushed context the same as {{this}} (unescaped, as it is not a string primitive).

Template

{{#with user }}{{ . }}{{/with}}

Context

{
    "user": {
        "name": "Alice"
    }
}

Output

{"name":"Alice"}

with-pop-restores-outer-context

After a with block ends, the outer context is restored and available to subsequent interpolations.

Template

{{ /name }}|{{#with user }}{{ name }}{{/with}}|{{ /name }}

Context

{
    "name": "Outer",
    "user": {
        "name": "Inner"
    }
}

Output

Outer|Inner|Outer

with-nested

With blocks can be nested, each pushing a new context onto the stack.

Template

{{#with a }}{{#with b }}{{ c }}{{/with}}{{/with}}

Context

{
    "a": {
        "b": {
            "c": "deep"
        }
    }
}

Output

deep

with-rfc6901-identifier

The identifier in a {{#with}} can be an RFC 6901 pointer.

Template

{{#with /a/b }}{{ c }}{{/with}}

Context

{
    "a": {
        "b": {
            "c": "deep"
        }
    }
}

Output

deep

with-dot-notation-identifier

The identifier in a {{#with}} can be dot-notation.

Template

{{#with a.b }}{{ c }}{{/with}}

Context

{
    "a": {
        "b": {
            "c": "deep"
        }
    }
}

Output

deep

with-this-identifier

{{#with this}} pushes the root context again, making it available to subsequent identifiers.

Template

{{#with this }}{{ name }}{{/with}}

Context

{
    "name": "Alice"
}

Output

Alice

with-array-top-rfc6901

When the pushed context is a JsonArray, RFC 6901 pointers into it work correctly.

Template

{{#with items }}{{ /0/name }}{{/with}}

Context

{
    "items": [
        {
            "name": "first"
        }
    ]
}

Output

first

with-array-top-this

When the pushed context is a JsonArray, {{this}} renders it as JSON.

Template

{{#with items }}{{ this }}{{/with}}

Context

{
    "items": [
        1,
        2,
        3
    ]
}

Output

[1,2,3]

with-missing-key-pushes-null-this-renders-null

When the identifier resolves to a missing key, null is pushed; {{this}} renders "null".

Template

{{#with absent }}{{ this }}{{/with}}

Context

{
    "name": "Alice"
}

Output

null

with-missing-key-then-identifier-is-null

When the pushed context is null, any non-this identifier yields null.

Template

{{#with absent }}{{ x }}{{/with}}

Context

{
    "name": "Alice"
}

Output

null

with-primitive-then-identifier-is-null

When the pushed context is a JsonPrimitive, any non-this identifier yields null.

Template

{{#with name }}{{ x }}{{/with}}

Context

{
    "name": "Alice"
}

Output

null

with-primitive-then-this-renders

When the pushed context is a JsonPrimitive, {{this}} renders it as a string.

Template

{{#with name }}{{ this }}{{/with}}

Context

{
    "name": "Alice"
}

Output

Alice

with-peer-blocks

Multiple with blocks at the same level each push and pop correctly, restoring the outer context between them.

Template

{{#with a }}{{ n }}{{/with}}-{{#with b }}{{ n }}{{/with}}

Context

{
    "a": {
        "n": "A"
    },
    "b": {
        "n": "B"
    }
}

Output

A-B

with-doubled-adjacent-with-same-key

Two adjacent {{#with}} blocks over the same key each push and pop independently; the second is unaffected by the first.

Template

{{#with u }}{{ n }}{{/with}}{{#with u }}{{ n }}{{/with}}

Context

{
    "u": {
        "n": "Q"
    }
}

Output

QQ

Source fixture: render/with-block.json