check out my new blog at gmarik.info/blog

Thursday, September 27, 2007

rails: optional locals values in partials

As you may know, rails' partial is a proper way for producing reusable view chunks

But sometimes you may need the partial to look different depending on contetext( say partial "_post" is used in "posts_list" and "single_post" templates). No problems - rails gives you :locals option

.rhtml

<%= render :partial => 'post', :locals => {:look_different => true} %>

And then you can do in parial like
_post.rhtml

<% if look_different %>
<p>look different <p>
<% else %>
<p>look usual <p>
<% end %>

But it gets painful passing all the locals all the time...otherwise you'll get NameError for not passing partial's option

Solution: optional locals values


Core rails team solution


just use local_assigns[:key] to access your local variables :)
Like
_post.rhtml

<% if local_assigns[:look_different] %>
<p>look different <p>
<% else %>
<p>look usual <p>
<% end %>

from rails 1.2.3 sources
from ActionView::Base docs

Labels: , , , ,

 check out my new blog at gmarik.info/blog

Monday, September 17, 2007

Google code uses jQuery!

Today, surfing around google code i accidentally stoped page loading and found pretty interesting js error:

a.feed.entries[b],e=r(d),f=g(m,e);
jQuery("#techtalks .posts").append(f)
...

Meaning that Google uses jQuery!

jQuery rox! :)

 check out my new blog at gmarik.info/blog

Friday, September 14, 2007

The button element

Here's my fix for a IE button hell(first 2 issues) i've created a while ago actually...

Useful links to read about the Button element:

 check out my new blog at gmarik.info/blog

Sunday, September 02, 2007

Rails: collection index within partial render

Tonight i've found a tiny and nice feature i was missing sometimes...(in order to have it created custom helper)

So when you have:
.rhtml:

<ul>
<%= render :partial => 'games/game', :collection => @games %>
</ul>


you may sometimes need to access collection index. And you can easily do that by using local variable named by such convention <partial_name>_counter
Like this:

.rhtml:

<li> <%= game_counter %> item
</li>


PS. and yes you can always use ordered(numbered)lists(ol instead ul)