Created
April 29, 2010 15:42
-
-
Save defunkt/383788 to your computer and use it in GitHub Desktop.
Mustache Existential Operator
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 'mustache' | |
module ExistentialMethodMissing | |
def method_missing(method, *args) | |
return super unless method.to_s =~ /\?$/ | |
val = send(method.to_s.chomp('?')) | |
val.respond_to?(:empty?) ? !val.empty? : !!val | |
end | |
def respond_to?(method) | |
super method.to_s.chomp('?') | |
end | |
end | |
class MyView < Mustache | |
include ExistentialMethodMissing | |
self.template = <<template | |
{{# people?}} | |
<ul> | |
{{# people}} | |
<li>{{ name }}</li> | |
{{/ people}} | |
</ul> | |
{{/ people?}} | |
template | |
def people | |
return [ | |
{ :name => "Chris" }, | |
{ :name => "Bob" }, | |
{ :name => "Mislav" } | |
] | |
end | |
end | |
puts MyView.render |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment