Created
January 31, 2012 21:18
-
-
Save cheald/1712970 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def handlebars_js(name, opts = {}, &block) | |
inner = capture(&block).gsub(/\s*\n\s*/, " ") | |
concat raw "window._templates['#{name}'] = #{inner.to_json};\n" | |
nil | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=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