Skip to content

Instantly share code, notes, and snippets.

@minikomi
Created August 18, 2011 09:47
Show Gist options
  • Save minikomi/1153760 to your computer and use it in GitHub Desktop.
Save minikomi/1153760 to your computer and use it in GitHub Desktop.
Monkey patch padrino partial renderer to tell when partials are rendered
module Padrino
module Helpers
module RenderHelpers
##
# Partials implementation which includes collections support
#
# ==== Examples
#
# partial 'photo/item', :object => @photo
# partial 'photo/item', :collection => @photos
# partial 'photo/item', :locals => { :foo => :bar }
# partial 'photo/item', :engine => :erb
#
def partial(template, options={})
logger.debug "PARTIAL: #{template} called"
options.reverse_merge!(:locals => {}, :layout => false)
path = template.to_s.split(File::SEPARATOR)
object_name = path[-1].to_sym
path[-1] = "_#{path[-1]}"
explicit_engine = options.delete(:engine)
template_path = File.join(path).to_sym
raise 'Partial collection specified but is nil' if options.has_key?(:collection) && options[:collection].nil?
if collection = options.delete(:collection)
options.delete(:object)
counter = 0
collection.map { |member|
counter += 1
options[:locals].merge!(object_name => member, "#{object_name}_counter".to_sym => counter)
render(explicit_engine, template_path, options.dup)
}.join("\n")
else
if member = options.delete(:object)
options[:locals].merge!(object_name => member)
end
render(explicit_engine, template_path, options.dup)
end
end
alias :render_partial :partial
end # RenderHelpers
end # Helpers
end # Padrino
@minikomi
Copy link
Author

Line 15 was the only thing I needed to add..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment