Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Abstractview implementation
module ViewMixin
def view_has?(key)
false
end
def view_section(key, contents)
# use #view_fetch to render section
end
# partial method, ...
end
class Hash
include ViewMixin
alias view_has? has_key?
alias view_fetch []
end
module ViewObject
include ViewMixin
alias view_has? respond_to?
def view_fetch(key)
meth = method(key)
if meth.arity.size==1
return meth.to_proc
else
return meth[]
end
end
end
#An example :
module MyHighLighterHelper
extend ViewMixin
def self.view_has?(language)
MyHighLighter.highlight?(language)
end
def self.view_fetch(language)
lambda { |text|
MyHighLighter.render(text, language)
}
end
end
myview = Object.new
myview.extend(ViewObject)
def myview.hl
MyHighLighterHelper
end
tpl = <<-END
{{#hl.ruby}}
#some ruby code
{{/hl.ruby}}
END
Mustache.render(tpl, myview)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment