Ensure your Ember templates get the correct name when pre-compiling in the Rake pipeline using Barber

I’ve been using Rake Pipeline as the build tool of choice for my Ember Phonegap Starter kit and making use of the Barber gem to pre-compile my Ember templates.

Today I ran into a problem where my templates were being compiled but they weren’t being given relative names if they were located in folders, so a template found at post/details.hbs was being inserted into Ember.TEMPLATES[‘details’] instead of Ember.TEMPLATES[‘post/details’]

Luckily, Adam Hawkins, was able to help me out, pointing out that I needed to supply a template name proc. The default is to use the absolute file name as shown here

My template files are all located in the ‘templates’ folder, so I modified my Assetfile to match handlebars templates and pre-compile them using the following instruction

match "templates/**/*.hbs" do
handlebars wrapper_proc: Barber::Ember::FilePrecompiler, :key_name_proc => ember_template_name_proc
view raw gistfile1.txt hosted with ❤ by GitHub

And here is the proc that will ensure your Ember templates are named correctly, stripping out the top level ‘templates’ folder but keeping the rest of the structure in the template name if it is present

ember_template_name_proc = proc { |input|
dir = File.dirname(input.path)
dir = dir.sub!('templates/', '')
template_name = File.basename(input.path, File.extname(input.path))
template_name = dir + '/' + template_name if !dir.nil?
view raw gistfile1.txt hosted with ❤ by GitHub
This entry was posted in Uncategorized and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s