Created
March 6, 2010 06:12
-
-
Save defunkt/323535 to your computer and use it in GitHub Desktop.
named arguments in mustache.rb
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
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