Skip to content

Instantly share code, notes, and snippets.

@aportnov
Created April 23, 2011 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aportnov/938576 to your computer and use it in GitHub Desktop.
Save aportnov/938576 to your computer and use it in GitHub Desktop.
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