开发者

Render partial in layout file with data

The navigation for my site is obviously stored in the application layout file. Part of that navigation is driven by the database. How to I render a partial in the layout and pass in the collection of objects for it to render?

EDIT: I think my question revolves more around how to get data into the partial, is that done form the application controller or do I have to add the data in each act开发者_运维百科ion on each controller?


If you just wish to pass one object in then you can use the object key on render. The passed object will be accessible as an local variable of the same name as the partial. SO if the partial is called navigation the local variable will be navigation.

<%= render :partial => 'foo/navigation', :object => @my_collection

In the partial:

<% for obj in navigation %>
  ...
<% end %>

If you wish to pass multiple objects then you can use the locals key. The names of the local variables in your layout are then the keys of the passed hash.

<%= render :partial => 'foo/navigation', :locals => { :foo => 'Hello', :bar => 'World' }

In the partial:

<%= foo %>
<%= bar %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜