With PHP you would have to:
{include file='links.tpl' title='Newest links' links=$link_array}Then in links.tpl do the loop:
<div id="box">But Rails on the other hand if you pass a collection to a partial it automatically treats each item in the collection as an individual item and there is no need to create a loop it is done inheritly.
<h3>{$title}{/h3>
<ul>
{foreach from=$links item=l}
.. do stuff ...
</foreach}
</ul>
</div>
Rails:
<%= render :partial => 'list_item', :collection => @customers %>_list_item.rhtml:
<tr class="<%= cycle('odd','even') %>">You may be thinking that it is really not much less work but writing loops all day long can get tiring especially when they are look the same and do the same thing. Partials take it to the next level and add a real intuitiveness to the loop because it is not just an array of items you are looping through it is actual objects a template is being rendered with.
<td><%= link_to list_item.last_name,
:controller => 'customer',
:action => 'show',
:id => list_item.id %></td>
<td><%= list_item.first_name %></td>
<td><%= list_item.city %>, <%= list_item.state %></td>
</tr>
Partials can also be used without the collection in just a basic include of a sub template the syntax is below. Useful but I have not found it as much fun.
<%= render :partial => "account", :locals => { :account => @buyer } %>
Blogged with Flock
No comments:
Post a Comment