Created
April 23, 2011 12:41
-
-
Save aportnov/938576 to your computer and use it in GitHub Desktop.
Using partials with builder in Sinatra
This file contains hidden or 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
module Sinatra | |
module BuilderHelper | |
def helper_modules | |
@helper_mods ||= Sinatra::Application.ancestors.select{|m| m.name =~ /Helper/} | |
end | |
def builder_partial(template, xml, locals = {}, helper_mods = nil) | |
template = "#{template}.builder" unless "#{template}" =~ /[.]builder$/ | |
helper_mods ||= self.helper_modules | |
class << (context = OpenStruct.new({:xml => xml}.merge(locals))); self; end.send(:include, *helper_mods) | |
File.open(File.join(settings.views, "#{template}")) do |f| | |
eval f.read, context.send(:binding) | |
end | |
end | |
end | |
helpers BuilderHelper | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment