Fork me on GitHub← back

Emblem.js

Syntax

Tags

You can wrap content within an html5 element by just writing the element name at the beginning of a line.

This is supported for all valid html5 tags (list here.)

To use a non-html5 tag, just start the line with %.

h1 Welcome to Emblem

%blink buy it on Kozmo.com
<h1>Welcome to Emblem</h1>

<blink>buy it on Kozmo.com</blink>

Indentation Wrapping

To wrap one element in another, just indent and place the element below.

footer
  ul
    li Hello
    li Goodbye
<footer>
  <ul>
    <li>Hello</li>
    <li>Goodbye</li>
  </ul>
</footer>

CSS Classes

CSS classes can added to elements by using a (.) with the class name afterwards. You can place this after an element name (div is the default tag name).

You can chain multiple class names.

.title Title

h1.logo Law Blog

button.btn.btn-large Submit
<div class="title">Title</div>

<h1 class="logo">Law Blog</h1>

<button class="btn btn-large">Submit</button>

IDs

Element IDs can be added to elements by using # before the element id. Like CSS classes, you can provide an element name (div is the default tag name).

#page-content Content

span#name Bob Lablah
<div id="page-content">Content</div>

<span id="name">Bob Lablah</span>

HTML Attributes

HTML attributes can be added right after the element, using key="value" pairs.

HTML attributes can also have mustaches embedded in them, though make sure to use the unbound helper in an Ember setting. Note the exclamation mark shorthand for setting an HTML attribute to an unbound property in an Ember setting.

button.close data-dismiss="modal" x

/ For Vanilla Handlebars mode only
button class="large {{foo}}" x

/ For Ember Handlebars
button class="large {{unbound foo}}" x

/ Shorthand for Ember
button class=foo! x
<button class="close" data-dismiss="dropdown">x</button>

<button class="large {{foo}}">x</button>

<button class="large {{unbound foo}}">x</button>

<button class="{{unbound foo}}">x</button>

Comments

Start a line with / to add a comment. These lines will be ignored completely.

Multiline comments are supported by indenting your comment underneath.

/ Some comment

/ A long long
  multiline comment

Plain Text

To output content without an element wrapper, start the line with pipe |.

Multiline plaintext content is supported by indenting underneath the tag or |.

Plain text can include handlebars output tags {{}} and {{{}}}. You can also use #{} as an alias for {{}}.

Block helpers within text blocks can also be used if you’re using the : colon or | pipe syntax.

Use an apostrophe ' instead of | to append a single trailing whitespace at the end of the text block.

| Some content

p
  | Lorem #{link-to 'something' | ipsum} dolor sit amet, consectetur 
    adipisicing elit, sed do eiusmod tempor incididunt ut labore et 
    dolore magna aliqua.  Ut enim ad minim veniam, quis nostrud 
    exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

span.name Your name is {{name}}
          and my name is #{author}

span
  ' Trailing space
Some content

<p>
  Lorem {{#link-to something}}ipsum{{/link-to}} dolor sit amet, 
  consectetur adipisicing elit, sed do eiusmod tempor incididunt
  ut labore et dolore magna aliqua.  Ut enim ad minim veniam, 
  quis nostrud exercitation ullamco laboris nisi ut aliquip 
  ex ea commodo consequat.
</p>

<span class="name">Your name is {{name}} and my name is {{author}}</span>

<span>Trailing space </span>

Handlebars Expressions

To output a handlebars expression, use = before expression.

NOTE: You have the option to omit the = unless the expression begins with a known HTML5 tag name.

h1 = name
p
  = intro
  = highlight name
<h1>{{name}}</h1>
<p>
  {{intro}}
  {{hightlight name}}
</p>

Unescaped Expressions

By default Handlebars html escapes output from expressions. To include html without escaping, use == which will output Handlebars “triple-stash” expressions {{{}}}.

body
  == outlet
<body>
  {{{outlet}}}
</body>

Handlebars Block Helpers

Handlebars block statements follow a syntax similar to HTML5 elements, in that indented content on the lines below get wrapped in the block form of the helper.

To use a block helper with the same name as an HTML5 element, you can explicitly use a handlebars block helper by starting the line with =.

Block helpers with only text content can be succinctly written on a single line using a | pipe to separate the helper code from the text.

ul
  each person in people
    li = person

link-to "home" | Link Text

list nav id="nav-bar" class="top"
  a href="url" = title

= strong
  = something

if something
  p Something!
else
  p Something else!
<ul>
  {{#each person in people}}
    <li>{{person}}</li>
  {{/each}}
</ul>

{{#link-to "home"}}
  Link Text
{{/link-to}}

{{list nav id="nav-bar" class="top"}}
  <a href="{{url}}">{{title}}</a>
{{/list}}

{{#strong}}
  {{something}}
{{/strong}}

{{#if something}}
  <p>Something!</p>
{{else}}
  <p>Something else!</p>
{{/if}}

Condensed Nesting with Colon Separator (NEW)

You can condense nested HTML or block mustache content into a single line by using : to terminate the present element or block mustache declaration.

Whatever comes after a statement-terminating : will be interpreted as if it were moved to the line below and indented. This allows you to quickly alternate between nesting HTML elements and mustache blocks on a single line when it makes sense.

This should come in handy for scaffolding frameworks like Twitter Bootstrap (the first part of the example on the right is Bootstrap scaffolding code).

#content-frame: .container: .row:
  .span4: render "sidebar"
  .span8: render "main"

footer: ul.menu-items: each menu_items: li: a.menu-link href=url: link_text
<div id="content-frame">
  <div class="container">
    <div class="row">
      <div class="span4">
        {{render "sidebar"}}
      </div>
      <div class="span8">
        {{render "main"}}
      </div>
    </div>
  </div>
</div>
<footer>
  <ul class="menu-items">
    {{#each menu_items}}
      <li>
        <!-- of course, in an Ember.js context, href will be set via a bind-attr -->
        <a class="menu-link" href="{{url}}">{{link_text}}</a>
      </li>
    {{/each}}
  </ul>
</footer>

ViewsEmber.js only

You can quickly and easily include an Ember.js view by starting a line with a capitalized letter. This will automatically wrap the line with a {{view}} helper.

.field
  Ember.TextField valueBinding="firstName"
<div class="field">
  {{view Ember.TextField valueBinding="firstName"}}
</div>

bind-attr ShorthandEmber.js only

Emblem.js makes binding expression attributes (via bind-attr) clean and simple.

img src=logoUrl alt="Logo"

div class=condition:whenTrue:whenFalse

div class={ condition1:whenTrue:whenFalse condition2:whenTrue:whenFalse }
<img {{bind-attr src="logoUrl"}} alt="Logo">

<div {{bind-attr class="condition:whenTrue:whenFalse"}}></div>

<div {{bind-attr class="condition1:whenTrue:whenFalse condition2:whenTrue:whenFalse"}}></div>

Action / Events ShorthandEmber.js only

Like with HTML5 elements, Emblem is aware of common Ember action names, such as click, submit, mouseEnter, etc. When you use these as attributes, they’ll be converted to action helpers.

a click="toggleHeader" x

a click="toggleHeader target='view'" x

a click="toggleHeader this" x

form submit="submitTheForm foo"
  p Hello
<a {{action toggleHeader on="click"}}>x</a>

<a {{action toggleHeader on="click" target="view"}}>x</a>

<a {{action toggleHeader this on="click"}}>x</a>

<form {{action submitTheForm foo on="submit"}}><p>Hello</p></form>

Explicit Attribute ExpressionsEmber.js only

To use expressions within the element attributes (often used in Ember with bind-attr and action), use the {} syntax after the element.

Multiple attribute expressions are also supported.

button{action "delete"} Delete

img{bind-attr src="logoUrl"} alt="logo"
a{bind-attr class="isActive"}{action 'toggleHeader'} x
<button {{action "delete"}}>Delete</button>

<img {{bind-attr src="logoUrl"}} alt="logo">

<a {{bind-attr class="isActive"}} {{action "toggleHeader"}}>x</a>

In-Tag Mustache

Occasionally, you’ll want to put mustache content inside the opening tag of an HTML element. You can do this by immediately following the tag content with curly braces.

Note: instead of using this for Ember’s bind-attr, you can use Emblem’s bind-attr syntax described below in Implicit Bind Attributes.

span.some-class{ someHelper } Hello
<span class="some-class" someHelperOutput="foo">Hello</span>

Vanilla Handlebars Partials

To invoke partials with non-Emberized Handlebars, you can use the > syntax.

Note that you’ll never really use this for Ember apps; rather, in those cases, you’d use the partial helper.

Also note that there’s no good way to precompile partials other than to precompile them as templates and then run Handlebars.partials = Handlebars.templates before any rendering has taken place.

If you’re not precompiling, and you want to directly register an Emblem template, you can use Emblem.registerPartial.

> partialName

p Check out this partial: #{> partialName}

> partialName foo
{{>partialName}}

<p>Check out this partial: {{>partialName}}</p>

{{>partialName foo}}

Anything wrong or missing here?

These docs are open source, so help us tweak and refine them!

If you’re feeling particularly lazy and want to just report an error, Submit a docs issue for some clarification on how Emblem.js can be used.

To see some other really cool stuff that Emblem is capable of, see the Idioms docs.