How to do collection partials

7 views
Skip to first unread message

francois.beausoleil

unread,
Jun 28, 2008, 12:25:41 AM6/28/08
to sinatrarb
Hi all!

I'm searching for the way to do the same thing as this in Rails:

<%= render :partial => "the_template", :collection => @collection %>

I can't find it in the README. There is a single mention of partials,
and it's not even a hyperlink.

Thanks!
François

blake.m...@gmail.com

unread,
Jun 28, 2008, 1:26:33 AM6/28/08
to sina...@googlegroups.com

Chris Schneider

unread,
Jun 29, 2008, 1:30:30 AM6/29/08
to sina...@googlegroups.com
I think the longer answer is "implement it yourself".  It shouldn't be hard really, just need to loop over the :collection argument in the partial helper (in the FAQ). In the end, sinatra has a totally different philosophy than something like rails.  Rails does everything for you, sinatra gives you the tools to do it yourself.  I doubt you'll ever see an ActiveSupport type library in Sinatra.

If you need more help, ping this list again, and we can work it out.

Heck, even if it's easy and trivial for you, ping the list and let us see the result.  It's awesome seeing working code form people.

Chris

francois.beausoleil

unread,
Jul 1, 2008, 12:51:24 AM7/1/08
to sinatrarb
Hello Chris, Blake,

Thanks for your replies. Here's my code:

def partial(template, *args)
options = args.extract_options!
if collection = options.delete(:collection) then
collection.inject([]) do |buffer, member|
buffer << erb(template, options.merge(:layout =>
false, :locals => {template.to_sym => member}))
end.join("\n")
else
erb(template, options)
end
end

I had to modify Sinatra itself to allow erb templates to access locals
(which seems unimplemented, as opposed to HAML). Here's the patch:

From c78da0d497bd3153115db18c6066a8ad4abda27a Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Fran=C3=A7ois=20Beausoleil?= <fran...@teksol.info>
Date: Tue, 1 Jul 2008 00:39:17 -0400
Subject: [PATCH] Allow ERB templates to hold locals.

---
sinatra-0.2.2/lib/sinatra.rb | 15 ++++++++++++++-
1 files changed, 14 insertions(+), 1 deletions(-)

diff --git a/sinatra-0.2.2/lib/sinatra.rb b/sinatra-0.2.2/lib/
sinatra.rb
index 8971926..bf7a68e 100644
--- a/sinatra-0.2.2/lib/sinatra.rb
+++ b/sinatra-0.2.2/lib/sinatra.rb
@@ -462,7 +462,20 @@ module Sinatra
private

def render_erb(content, options = {})
- ::ERB.new(content).result(binding)
+ metaclass = class << self; self; end
+ (options[:locals] || Hash.new).each do |key, value|
+ metaclass.send(:attr_accessor, key)
+ send("#{key}=", value)
+ end
+
+ begin
+ ::ERB.new(content).result(binding)
+ ensure
+ (options[:locals] || Hash.new).each do |key, value|
+ metaclass.send(:undef_method, "#{key}=")
+ metaclass.send(:undef_method, key)
+ end
+ end
end

end
--
1.5.5.1

Do you want me to fork and add tests, or will you use the code as-is?

Thanks!
François

On 29 juin, 01:30, "Chris Schneider" <jerk...@gmail.com> wrote:
> I think the longer answer is "implement it yourself".  It shouldn't be hard
> really, just need to loop over the :collection argument in the partial
> helper (in the FAQ). In the end, sinatra has a totally different philosophy
> than something like rails.  Rails does everything for you, sinatra gives you
> the tools to do it yourself.  I doubt you'll ever see an ActiveSupport type
> library in Sinatra.
>
> If you need more help, ping this list again, and we can work it out.
>
> Heck, even if it's easy and trivial for you, ping the list and let us see
> the result.  It's awesome seeing working code form people.
>
> Chris
>
> On Fri, Jun 27, 2008 at 11:26 PM, <blake.mizer...@gmail.com> wrote:
>
> > see sinatrarb.com/FAQ
> > On 6/27/08, francois.beausoleil <francois.beausol...@gmail.com> wrote:
>
> > > Hi all!
>
> > > I'm searching for the way to do the same thing as this in Rails:
>
> > > <%= render :partial => "the_template", :collection => @collection %>
>
> > > I can't find it in the README.  There is a single mention of partials,
> > > and it's not even a hyperlink.
>
> > > Thanks!
> > > François
>
> > --
> > Blake Mizerany
> > blake.mizer...@gmail.com

Chris Schneider

unread,
Jul 1, 2008, 12:13:28 PM7/1/08
to sina...@googlegroups.com
I like the partial code you have, it's pretty much what I would have written. Can you throw a second FAQ entry up on the wiki with it? (keep that first partial example simple)

Chris
Reply all
Reply to author
Forward
0 new messages