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

Magic Tag Types

  • Edit on GitHub

Rendering behavior is controlled with magic tags that look like {{}}. There are two main forms of magic tags:

  • Insertion tags - insert their value into the result like {{expression}} and {{{expression}}}.
  • Section tags - optional render a sub-section like {{#expression}} ... {{/expression}}.

Let’s see the general behavior of each tag type:

Insertion Tags

Insertion tags render a value into result.

{{expression}}

Inserts the escaped value of expression into the result. This is the most common tag.

<!-- Template -->
<div>{{name}}</div>
/* Data */
{ name: "<b>Justin</b>" }
<!-- Result -->
<div>&lt;b&gt;Justin&lt;/b&gt;</div>

{{{expression}}}

Inserts the unescaped value of expression into the result.

<!-- Template -->
<div>{{{name}}}</div>
/* Data */
{ name: "<b>Justin</b>" }
<!-- Result -->
<div><b>Justin</b></div>

{{>key}}

Renders another template with the same context as the current context.

var template = stache("<h1>{{>title}}</h1>");


var frag = template(
    {message: "Hello"},
    {
        partials: { title:  stache("<blink>{{message}}</blink>") }
    });

    frag //-> <h1><blink>Hello</blink></h1>

Other ways to load and reference partials are discussed here.

{{!expression}}

Ignores the magic tag.

<!-- Template -->
<h1>{{!message}}</h1>
/* Data */
{ message: "<blink>Hello</blink>" };
<!-- Result -->
<h1></h1>

Section Tags

Section tags are passed a subsection and an optional inverse subsection. They optionally render the subsections and insert them into the result.

{{#expression}} ... {{/expression}}

Renders the subsection or inverse subsection depending on the value of expression.

If expression is truthy, renders the subsection:

<!-- Template -->
<h1>{{#shown}}Hello{{/shown}}</h1>
/* Data */
{ shown: true };
<!-- Result -->
<h1>Hello</h1>

The subsection is rendered with the expression value as the top of the scope:

<!-- Template -->
<h1>{{#person}}Hello {{first}}  {{person.last}}{{/person}}</h1>
/* Data */
{ person: {first: "Alexis", last: "Abril"} };
<!-- Result -->
<h1>Hello Alexis Abril</h1>

If expression is falsey, renders the inverse subsection if present:

<!-- Template -->
<h1>{{#shown}}Hello{{else}}Goodbye{{/shown}}</h1>
/* Data */
{ shown: false };
<!-- Result -->
<h1>Goodbye</h1>

If expression is array-like and its length is greater than 0, the subsection is rendered with each item in the array as the top of the scope:

<!-- Template -->
<p>{{#items}}{{.}} {{/items}}</p>
/* Data */
{items: [2,4,8,16]}
<!-- Result -->
<p>2 4 8 16 </p>

If expression is array-like and its length is 0, the inverse subsection is rendered:

<!-- Template -->
<p>{{#items}}{{.}} {{else}}No items{{/items}}</p>
/* Data */
{items: []}
<!-- Result -->
<p>No items</p>

{{^expression}} ... {{/expression}}

The inverse section does the opposite of the normal {{#expression}} tag. That is, it renders the subsection when {{#expression}} would render the inverse subsection and it renders the inverse subsection when {{#expression}} would render the subsection.

<!-- Template -->
<h1>{{^shown}}Hello{{/shown}}</h1>
/* Data */
{ shown: false };
<!-- Result -->
<h1>Hello</h1>

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