Skip to content

Instantly share code, notes, and snippets.

@defunkt
Created April 29, 2010 15:42
Show Gist options
  • Save defunkt/383788 to your computer and use it in GitHub Desktop.
Save defunkt/383788 to your computer and use it in GitHub Desktop.
Mustache Existential Operator
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