Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
named arguments in mustache.rb
require 'rubygems'
require 'mustache'
class HandleBar < Mustache
def respond_to?(methodname)
methodname = methodname.to_s.split(/\s+/).first
super methodname
end
def method_missing(methodname, *args)
arguments = methodname.to_s.split(/\s+/)
methodname = arguments.shift
arguments = arguments.join(' ').split(/\b\s/).inject({}) do |hsh, pair|
key, value = pair.split(/:\s*/)
hsh.update(key => value)
end
send methodname, arguments
end
def wax_me(args)
"you've been waxed! one: #{args['one']}, two: #{args['two']}, three: #{args['three']}"
end
end
if $0 == __FILE__
h = HandleBar.new
h.template = "Mustache doesn't do arguments you say?\n\n{{wax_me one: 1 two: 2 three: three}}"
puts h.to_html
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment