Skip to content

Instantly share code, notes, and snippets.

@ppcano
Last active November 11, 2023 06:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppcano/4945826 to your computer and use it in GitHub Desktop.
Save ppcano/4945826 to your computer and use it in GitHub Desktop.
Precompile handlebars templates with rake-pipeline
input "app" do
match "templates/**/*.handlebars" do
filter HandlebarsPrecompiler
filter ConcatFilter, "apptmp.js"
filter AddMicroLoader, :global => true
filter AddHandlebarsDependencies
end
.....
match "vendor/handlebars.runtime-1.0.rc.2.js" do
filter ConcatFilter, "apptmp.js"
end
end
class AddHandlebarsDependencies < Rake::Pipeline::Filter
def generate_output(inputs, output)
contents = <<END
minispade.require('rsvp');
minispade.require('container');
minispade.require('ember-debug');
minispade.require('ember-metal');
minispade.require('ember-runtime');
minispade.require('ember-application');
minispade.require('ember-views');
minispade.require('ember-states');
minispade.require('metamorph');
minispade.require('ember-handlebars');
END
output.write contents
inputs.each do |input|
output.write input.read
end
end
end
class HandlebarsPrecompiler < Rake::Pipeline::Filter
class << self
def context
unless @context
contents = <<END
#{File.read("./app/submodules/ember.js/packages/handlebars/lib/main.js")}
#{File.read("./app/vendor/precompile/ember-runtime.js")}
#{File.read("./app/submodules/ember.js/packages/ember-handlebars-compiler/lib/main.js")}
function precompileEmberHandlebars(string) {
return Ember.Handlebars.precompile(string).toString();
}
END
@context = ExecJS.compile(contents)
end
@context
end
end
def precompile_templates(name, data)
"\nEmber.TEMPLATES['#{name}'] = Ember.Handlebars.template(#{self.class.context.call("precompileEmberHandlebars", data)});\n"
end
def generate_output(inputs, output)
inputs.each do |input|
name = File.basename(input.path, '.handlebars')
data = File.read(input.fullpath)
result = precompile_templates(name, data)
output.write result
end
end
end
Em.View.extend({
elementId: 'about_screen',
templateName: 'about_screen'
});
/// use the file -> about_screen.handlebars
@ppcano
Copy link
Author

ppcano commented Feb 13, 2013

My project directory is something like:

app

lib
submodules

ember.js
data
...

vendor

precompile/.. ( libraries required to precompile templates )

To precompile handlebars template in the server side, some steps where necessary:

  • Building an ember-runtime version to be used in the HandlebarsPrecompiler filter.
  • Add the require dependencies of the precompile templates before their content appending the AddHandlebarsDependencies filter.

Any other recommend way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment