DoneJS StealJS jQuery++ FuncUnit DocumentJS
3.14.1
5.0.0 4.3.0 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-compute
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-stream
      • can-define-stream-kefir
      • can-event
      • can-event/async/async
      • can-event/batch/batch
      • can-event/lifecycle/lifecycle
      • can-kefir
      • can-list
      • can-map
      • can-map-backup
      • can-map-define
      • can-observation
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
    • Data Modeling
      • can-connect
      • can-connect-cloneable
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-signalr
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
        • Pages
          • Magic Tag Types
          • Scope and Context
          • Expressions
          • Template Acquisition
          • Helpers
          • Live Binding
          • Whitespace Control
          • Sections
        • Methods
          • addHelper
          • addLiveHelper
          • from
          • registerConverter
          • registerHelper
          • registerPartial
          • registerSimpleHelper
          • safeString
        • Tags
          • {{expression}}
          • {{{expression}}}
          • {{#expression}}
          • {{/expression}}
          • {{^expression}}
          • {{>key}}
          • {{!expression}}
          • {{<partialName}}
          • {{else}}
        • Expressions
          • Bracket Expression
          • Call Expression
          • Hash Expression
          • Helper Expression
          • KeyLookup Expression
          • Literal Expression
        • Key Operators
          • @at
          • ~compute
          • ./current
          • ../parent
          • scope
          • %special
          • this
          • *variable
          • *self
          • key
        • Helpers
          • {{#if(expression)}}
          • {{#unless(expression)}}
          • {{#each(expression)}}
          • {{#with(expression)}}
          • {{log()}}
          • {{debugger()}}
          • {{#eq(expressions)}}
          • {{#is(expressions)}}
          • {{#switch(expression)}}
          • {{#case(expression)}}
          • {{#default()}}
          • {{joinBase(expressions)}}
        • Types
          • getterSetter
          • helper
          • helperOptions
          • renderer
          • sectionRenderer
          • simpleHelper
      • can-stache/helpers/route
      • can-stache-bindings
      • can-stache-converters
      • can-view-autorender
      • can-view-callbacks
      • can-view-href
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-globals
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-control
      • can-dom-events
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-jquery
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-namespace
      • can-reflect
      • 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

Insert the value of the expression into the output of the template.

{{EXPRESSION}}

Gets the value of EXPRESSION and inserts the result into the output of the template.

If the expression is clearly of a particular expression type like: {{myHelper arg}} or {{myMethod(arg)}}, that expression’s rules will be followed.

An ambiguous expression type like {{keyOrHelper}} will first treat keyOrHelper as a KeyLookup Expression and if there is no value in the scope of keyOrHelper, it will be treated as a Helper Expression.

Parameters

  1. expression {Literal Expression|KeyLookup Expression|Call Expression|Helper Expression}:

    The expression can be:

    • Literal Expression - {{5}} - Inserts a string representation of the literal.
    • KeyLookup Expression - {{key}} - Looks up the value of key in the can-view-scope.
    • Call Expression - {{method()}} - Calls method in the can-view-scope.
    • Helper Expression - {{helper arg}} - Calls helper in the Options and passes it a helperOptions.

Use

The following breaks down the behavior of {{expression}}. It groups the behavior of KeyLookup Expression and Call Expressions because their behavior works the same way. It then details how Helper Expressions work.

Key and Call Expressions

{{key}} insert data into the template. It most commonly references values within the current context. For example:

Rendering:

<h1>{{name}}</h1>

With:

{name: "Austin"}

Results in:

<h1>Austin</h1>

If the key value is a String or Number, it is inserted into the template. If it is null or undefined, nothing is added to the template.

Nested Properties

Stache supports nested paths, making it possible to look up properties nested deep inside the current context. For example:

Rendering:

<h1>{{book.author}}</h1>

With:

{
  book: {
    author: "Ernest Hemingway"
  }
}

Results in:

<h1>Ernest Hemingway</h1>

Looking up values in parent contexts

Sections and block helpers can create their own contexts. If a key’s value is not found in the current context, it will look up the key’s value in parent contexts. For example:

Rendering:

{{#chapters}}
   <li>{{title}} - {{name}}</li>
{{chapters}}

With:

{
  title: "The Book of Bitovi"
  chapters: [{name: "Breakdown"}]
}

Results in:

<li>The Book of Bitovi - Breakdown</li>

Helper expressions

The {{helper}} syntax is used to call out to stache helper functions functions that may contain more complex functionality. helper is a key that must match either:

  • a registered helper function, or
  • a function in the current or parent contexts

The following example shows both cases.

The template:

<p>{{greeting}} {{user}}</p>

Rendered with data:

{
  user: function(){ return "Justin" }
}

And with a registered helper like:

stache.registerHelper('greeting', function() {
  return "Hello";
});

Results in:

<p>Hello Justin</p>

Arguments

Arguments can be passed from the template to helper function by listing space separated strings, numbers or other keys after the helper name. For example:

The template:

<p>{{madLib "Lebron James" verb 4}}</p>

Rendered with:

{verb: "swept"}

Will call a madLib helper with the following arguments:

stache.registerHelper('madLib',
  function(subject, verb, number, options){
    // subject -> "Lebron James"
    // verb -> "swept"
    // number -> 4
});

If an argument key value is a can-map property, the Observe’s property is converted to a getter/setter compute. For example:

The template:

<p>What! My name is: {{mr user.name}}</p>

Rendered with:

{user: new Map({name: "Slim Shady"})}

Needs the helper to check if name is a function or not:

stache.registerHelper('mr',function(name){
  return "Mr. "+ (typeof name === "function" ?
                  name():
                  name)
});

This behavior enables two way binding helpers and is explained in more detail on the helper functions docs.

Hash

If enumerated arguments isn’t an appropriate way to configure the behavior of a helper, it’s possible to pass a hash of key-value pairs to the helper option argument’s hash object. Properties and values are specified as hashProperty=hashValue. For example:

The template:

<p>My {{excuse who=pet how="shredded"}}</p>

And the helper:

stache.registerHelper("excuse",function(options) {
  return ["My",
    options.hash.who || "dog".
    options.hash.how || "ate",
    "my",
    options.hash.what || "homework"].join(" ");
});

Rendered with:

{pet: "cat"}

Results in:

<p>My cat shredded my homework</p>

Returning an element callback function

If a helper returns a function, that function is called back after the template has been rendered into DOM elements. This can be used to create stache tags that have rich behavior. Read about it on the helper function page.

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

On this page

Get help

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