Skip to content

Instantly share code, notes, and snippets.

@bxjx
Created May 12, 2011 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bxjx/968173 to your computer and use it in GitHub Desktop.
Save bxjx/968173 to your computer and use it in GitHub Desktop.
render js templates in rails
# in controller:
# @item = JsTemplate.new('item', :renderer => :mustache)
# in view:
# %span=@item.user.name
# 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