Skip to content

Instantly share code, notes, and snippets.

@cheald
Created January 31, 2012 21:18
Show Gist options
  • Save cheald/1712970 to your computer and use it in GitHub Desktop.
Save cheald/1712970 to your computer and use it in GitHub Desktop.
def handlebars_js(name, opts = {}, &block)
inner = capture(&block).gsub(/\s*\n\s*/, " ")
concat raw "window._templates['#{name}'] = #{inner.to_json};\n"
nil
end
window._templates = {};
- handlebars_js "post" do
%h1
=link_to "{{title}}", "/stories/{{id}}-{{slug}}/", :target => "_top"
- link_to "/stories/{{id}}-{{slug}}/", :target => "_top" do
%img.thumb{:src => "{{images.i300x200}}"}
.body
.excerpt
{{{content.excerpt}}}
%footer
%time {{post_date}}
by {{author}}
%span.shares {{shares.total}} shares
:plain
window._template_cache = {};
for(var name in window._templates) {
if(window._templates.hasOwnProperty(name)) {
window._template_cache[name] = Handlebars.compile(window._templates[name]);
}
}
=javascript_include_tag "/whatever/templates.js"
#anchor
:javascript
$("#anchor").html _template_cache["post"]({title: "Yay!", content: {excerpt: "More yay!"}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment