Created
May 12, 2011 08:33
-
-
Save bxjx/968173 to your computer and use it in GitHub Desktop.
render js templates in rails
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
# in controller: | |
# @item = JsTemplate.new('item', :renderer => :mustache) | |
# in view: | |
# %[email protected] | |
# rendered: | |
# {{item.user.name}} | |
class JsTemplate | |
def initialize(id, options = {}) | |
parents = options[:parents] || {} | |
@renderer = options[:renderer] | |
@path = parents + [id] | |
end | |
def method_missing(sym, *args, &block) | |
self.class.new(sym.to_s, @path) | |
end | |
def to_s | |
json_path = @path.join('.') | |
renderer == :mustache ? "{{#{json_path}}}" : "<%=#{json_path}%>" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment