Reputation: 8006
I have HAML installed with Rails. I'm trying to extend the functionality of it by making snippet-templates. For example I have this general image structure:
- @images.each do |image|
.image
%a{ :href => image.target }
= image_tag image.url
%a{ :href => edit_image_path(image) }
Edit
or at least something to that effect. What I would like to do is clean it up so that I can say:
- @images.each do |image|
= render 'snippet/image' image
How can I accomplish this semantically? Obviously, I could declare a variable in my snippet and assign it before rendering, but that seems super-lazy. Is there an example I could see that shows both the snippet and how it's rendered?
Upvotes: 2
Views: 87
Reputation: 17528
render
can take a :collection
option, like so:
= render :partial => "image", :collection => @images
See section 3.4.5 Rendering Collections in the Layouts and Rendering in Rails guide, which says:
the partial will be inserted once for each member in the collection ... the partial [will] have access to the member of the collection ... via a variable named after the partial
Upvotes: 3
Reputation: 1786
You can do this:
- @images.each do |image|
render :partial => 'snippet/image', :locals => { :image => image }
It's semantic alright, although a bit verbose.
Upvotes: 2