Skip to content

Conformance Examples 0.1.0 — Render: if-block

if-doubled-adjacent-if-same-key

Two adjacent {{#if}} blocks over the same key render independently; the second is unaffected by the first.

Template

{{#if flag }}A{{/if}}{{#if flag }}B{{/if}}

Context

{
    "flag": true
}

Output

AB

if-else-if-all-falsy-no-else-emits-nothing

Every arm falsy and no else arm: nothing is emitted.

Template

[{{#if a }}A{{else if b }}B{{else if c }}C{{/if}}]

Context

{
    "a": false,
    "b": 0,
    "c": ""
}

Output

[]

if-else-if-all-falsy-with-else-renders-else

Every arm falsy with an else arm present: the else body renders.

Template

{{#if a }}A{{else if b }}B{{else if c }}C{{else}}Z{{/if}}

Context

{
    "a": false,
    "b": 0,
    "c": ""
}

Output

Z

if-else-if-first-arm-wins

First arm truthy: its body renders and later arms are skipped.

Template

{{#if a }}A{{else if b }}B{{else if c }}C{{else}}Z{{/if}}

Context

{
    "a": true,
    "b": true,
    "c": true
}

Output

A

if-else-if-second-arm-wins-when-first-falsy

First arm falsy, second arm truthy: second arm's body renders.

Template

{{#if a }}A{{else if b }}B{{else if c }}C{{else}}Z{{/if}}

Context

{
    "a": false,
    "b": true,
    "c": true
}

Output

B

if-else-if-third-arm-wins-when-first-two-falsy

First two arms falsy, third truthy: third arm's body renders.

Template

{{#if a }}A{{else if b }}B{{else if c }}C{{else}}Z{{/if}}

Context

{
    "a": false,
    "b": false,
    "c": true
}

Output

C

if-empty-array-is-falsy

An empty JsonArray is falsy: the else arm renders.

Template

{{#if items }}yes{{else}}no{{/if}}

Context

{
    "items": []
}

Output

no

if-empty-object-is-truthy

An empty JsonObject is truthy (matches Handlebars): the body renders.

Template

{{#if obj }}yes{{else}}no{{/if}}

Context

{
    "obj": {}
}

Output

yes

if-empty-string-is-falsy

An empty string is falsy: the else arm renders.

Template

{{#if s }}yes{{else}}no{{/if}}

Context

{
    "s": ""
}

Output

no

if-false-no-else-emits-nothing

Identifier resolves to false and there is no else arm: emits nothing.

Template

[{{#if flag }}yes{{/if}}]

Context

{
    "flag": false
}

Output

[]

if-false-with-else-renders-else

Identifier resolves to false and an else arm is present: the else arm renders.

Template

{{#if flag }}yes{{else}}no{{/if}}

Context

{
    "flag": false
}

Output

no

if-jsonnull-is-falsy

An explicit JsonNull value is falsy.

Template

{{#if flag }}yes{{else}}no{{/if}}

Context

{
    "flag": null
}

Output

no

if-missing-key-treated-as-falsy

A missing key resolves to null and is treated as falsy: the else arm renders.

Template

{{#if absent }}yes{{else}}no{{/if}}

Context

{
    "flag": true
}

Output

no

if-multiple-spaces-between-else-and-if

Whitespace between 'else' and 'if' is permitted (spaces and tabs).

Template

{{#if a }}A{{else  if b }}B{{/if}}

Context

{
    "a": false,
    "b": true
}

Output

B

if-nested-inside-each-with-parent-pointer

Sanity: {{#if}} inside {{#each}} can address the current item and a parent value.

Template

{{#each items }}{{#if active }}[{{ this/name }}]{{/if}}{{/each}}

Context

{
    "items": [
        {
            "name": "a",
            "active": true
        },
        {
            "name": "b",
            "active": false
        },
        {
            "name": "c",
            "active": true
        }
    ]
}

Output

[a][c]

if-true-renders-body

Identifier resolves to true: the body renders.

Template

{{#if flag }}yes{{/if}}

Context

{
    "flag": true
}

Output

yes

if-zero-is-falsy

The number 0 is falsy: the else arm renders.

Template

{{#if n }}yes{{else}}no{{/if}}

Context

{
    "n": 0
}

Output

no

Source fixture: render/if-block.json