DoneJS StealJS jQuery++ FuncUnit DocumentJS
5.33.3
6.0.0 4.3.0 3.14.1 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-bind
      • can-compute
      • can-debug
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-compat
      • can-map-define
      • can-observable-array
      • can-observable-object
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
    • Views
      • can-attribute-observable
      • can-component
      • can-stache
        • Tags
          • {{expression}}
          • {{{expression}}}
          • {{#expression}}
          • {{/expression}}
          • {{else}}
          • {{<partialName}}
          • {{!expression}}
          • {{-expression-}}
        • Helpers
          • and
          • console
          • debugger
          • domData
          • eq
          • for(of)
          • portal
          • if
          • joinBase
          • let
          • not
          • or
          • switch
          • case
          • default
        • Expressions
          • Bracket Expression
          • Call Expression
          • Hash Expression
          • KeyLookup Expression
          • Literal Expression
        • Methods
          • addBindings
          • addConverter
          • addHelper
          • addLiveHelper
          • from
          • safeString
        • Key Operators
          • ~compute
          • ./current
          • ../parent
          • scope
          • scope/key
          • this
          • key
        • Pages
          • Expressions
          • Helpers
        • Types
          • getterSetter
          • helper
          • helperOptions
          • sectionRenderer
          • simpleHelper
          • view
        • Deprecated
          • Helper Expression
          • scope.vars
          • {{data name}}
          • {{#each(expression)}}
          • {{#is(expressions)}}
          • {{#unless(expression)}}
          • {{#with(expression)}}
          • registerConverter
          • registerHelper
          • registerPartial
          • Legacy Scope Behavior
          • {{^expression}}
          • {{>key}}
      • can-stache-bindings
      • can-stache-converters
      • can-stache-element
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • steal-stache
    • Data Modeling
      • can-connect
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-tag
      • can-fixture
      • can-fixture-socket
      • can-local-store
      • can-memory-store
      • can-ndjson-stream
      • can-query-logic
      • can-realtime-rest-model
      • can-rest-model
      • can-set-legacy
      • can-super-model
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • Data Validation
      • can-define-validate-validatejs
      • can-type
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

{{#expression}}

  • Edit on GitHub

Renders a subsection one or more times depending on the type of expression or the expression’s return value.

{{# EXPRESSION }} TRUTHY {{else}} FALSY {{/ EXPRESSION }}

Defines a TRUTHY section and optional FALSY expression. TRUTHY or FALSEY are rendered zero or many times depending on the behavior of EXPRESSION.

The following example uses {{#}} and {{/}} to define a section that is rendered conditionally by the if helper:

import {stache} from "can";

const view = stache(`<div>
  {{# if(this.day) }}
    Good Morning!
  {{/ if }}
  </div>`);

var fragment = view({day: true});
console.log(fragment.firstChild.innerHTML) //-> "Good Morning!"

document.body.appendChild(fragment);

Different helpers can render these sections multiple times. {{else}} also defines a FALSY section that helpers can call. In the following, for(of) calls the FALSY section:

import {stache} from "can";

const view = stache(`<ul>
  {{# for(value of values) }}
    <li>{{ value }}<li>
  {{ else }}
    <li>no values<li>
  {{/ for }}
  </ul>`);

var fragment = view({values: []});
console.log(fragment.firstChild.innerHTML) //-> "<li>no values</li>"

document.body.appendChild(fragment);

Helpers can control the variables and context accessible in the TRUTHY and FALSY section. Read addHelper and addLiveHelper on how to do this with your own helpers.

{{# KEY_EXPRESSION }} TRUTHY {{else}} FALSY {{/KEY_EXPRESSION}}

Deprecated 4.15.0

This use has been deprecated in favor of using if, for(of), or let.

  • Instead of {{#this.truthy}} {{../value}} {{/this.truthy}}, use {{# if(this.truthy) }} {{ this.value }} {{/ if }}.
  • Instead of {{#this.values}} {{value}} {{/this.values}}, use {{# for(value of this.values) }} {{ value }} {{/ for }}.
  • Instead of {{#this.object}} {{objectKey}} {{/this.object}}, use {{# let obj=this.object }} {{ obj.objectKey }} {{/ let }}.

Renders the FN or INVERSE section one or many times depending on the value in KEY_EXPRESSION.

If KEY_EXPRESSION returns an array like object, the FN section will be rendered for each item in the array. If the array like object is empty, the INVERSE section will be rendered. The {{#each(expression)}} helper should generally be used for observable array-like objects as it has some performance advantages.

{{#items}}<li>{{name}}</li>{{/items}}

If KEY_EXPRESSION returns a truthy value, the FN section will be rendered with the truthy value.

If KEY_EXPRESSION returns a fasley value, the INVERSE section will be rendered with the fasley value.

{{#address}} {{street}} {{city}} {{/address}}

The closing tag can end with {{/}}.

Parameters

  1. KEY_EXPRESSION {KeyLookup Expression}:

    A key expression. If there is no value in the scope of keyOrHelper, it will be treated as a Helper Expression.

  2. FN {sectionRenderer(context, helpers)}:

    The truthy subsection.

  3. INVERSE {sectionRenderer(context, helpers)}:

    An optional inverse section created by using {{else}}.

Use

{{#}} and {{/}} are used to define sections so helpers like if and for(of) can call those sections. This is similar to { and } in JavaScript.

For example:

{{# if(this.day) }}
  Good Morning!
{{/ if }}

is like:

if(this.day) {
    console.log( "Good Morning!" );
}

The helper called by {{#}} controls the variables and context accessible in the TRUTHY and FALSY sections. Read the helper's documentation to understand when the subsections are called and what they are called with.

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 5.33.3.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news