Created
May 29, 2009 09:30
-
-
Save lenary/119874 to your computer and use it in GitHub Desktop.
This file contains 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
# stolen from http://github.com/cschneid/irclogger/blob/master/lib/partials.rb | |
# and made a lot more robust by me | |
# this implementation uses erb by default. if you want to use any other template mechanism | |
# then replace `erb` on line 13 and line 17 with `haml` or whatever | |
module Sinatra::Partials | |
def partial(template, *args) | |
template_array = template.to_s.split('/') | |
template = template_array[0..-2].join('/') + "/_#{template_array[-1]}" | |
options = args.last.is_a?(Hash) ? args.pop : {} | |
options.merge!(:layout => false) | |
locals = options[:locals] || {} | |
if collection = options.delete(:collection) then | |
collection.inject([]) do |buffer, member| | |
buffer << erb(:"#{template}", options.merge(:layout => | |
false, :locals => {template_array[-1].to_sym => member}.merge(locals))) | |
end.join("\n") | |
else | |
erb(:"#{template}", options) | |
end | |
end | |
end |
I went with your approach (above using send) in the end when I sent a pull request to the Sinatra-Partial gem maintainer. The request has been merged, so it won't be long until there's another gem release i hope.
I recommend that you swap over to using the gem. It's unobtrusive and I'll make an effort to support any changes etc. First up on my list: a test suite and travis-ci.
And yes, I agree `render` felt a little low-level for my needs too. Soon I'll find an approach I prefer, but a priority is tests.
…On 10 Apr 2012, at 03:23 PM, Carlos ***@***.*** wrote:
I spent some time studying base.rb before making my implementation, and I considered using render directly, but it I personally did not like the idea of bypassing the built-in template rendering methods.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/119874
Excellent! I will be switching my current project over to using the gem. Thanks for all of the great work!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I spent some time studying base.rb before making my implementation, and I considered using render directly, but it I personally did not like the idea of bypassing the built-in template rendering methods.