{{#if key}}

  • function
can.stache.helpers.if  

{{#if key}}BLOCK{{/if}}

Renders the BLOCK template within the current template.

Parameters

  1. key {key}

    A key that references a value within the current or parent context. If the value is a function or can.compute, the function's return value is used.

  2. BLOCK {can.stache(template)}

    A stache template.

Returns

{String}

If the key's value is truthy, the BLOCK is rendered with the current context and its value is returned; otherwise, an empty string.

Use

{{#if key}} provides explicit conditional truthy tests. For example,

The template:

{{#if user.isFemale}}
  {{#if user.isMarried}}
    Mrs
  {{/if}}
  {{#if user.isSingle}}
    Miss
  {{/if}}
{{/if}}

Rendered with:

{user: {isFemale: true, isMarried: true}}

Results in:

Mrs

If can be used with {{else}} too. For example,

{{#if user.isFemale}}
  {{#if user.isMarried}}
    Mrs
  {{else}}
    Miss
  {{/if}}
{{/if}}

Rendered with:

{user: {isFemale: true, isMarried: false}}

Results in:

Miss