Ember Handlebars templates (Rails-like partials)

Ember Handlebars templates (Rails-like partials)

avatar

BETTINA STEGER

September 24, 2012

Javascript

I was looking for Handlebars partials (or templates that i could embed) that I can render like partials in Rails:

render :partial => "partial_name"

1. registerPartial in js

You can either register a partial for a small inline code snippet:

/* partials.js */
Handlebars.registerPartial("time",
  '<time datetime="{{created_at}}">{{created_at}}</time>'
);
/* file.handlebars */
some text created at: {{> time}}

2. Template File (.handlebars)

or the more convenient way: create a new template file (eg. time.handlebars) and include it with Ember.Handlebars helper „template“, like so:

/* time.handlebars */
<time datetime="{{created_at}}">{{created_at}}</time>

/* file.handlebars */
some text created at: {{template "path/to/file/time"}}

That’s it, pay attention to the right path otherwise Ember will not find the template (just see the console for the error message)!

Recent Blog Articles