Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Using partials with builder in Sinatra
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