Partials every-where

March 3, 2010 at 11:57 am (Partials) (, , , )

Hi everybody,

Partials are not limited to be used for HTML rendering.

They can be used for XML too.

Here is an example file ( _details.builder )
xml.gender user.gender
xml.age user.age

This can be used in another regular builder file as follows:
xml.instruct! : xml, :version=>”1.0″
xml.users {
for user in users
xml <“users/details” , :locals=>{:user=>user})

Hope, this comes handy 🙂

Permalink 1 Comment

passing optional parameters to partials

May 13, 2009 at 4:38 pm (Partials) (, , , )

Today i got an instance where i want to use a parameter in the partial, which may or may not be passed.This partial is called from two places, one which passes while other does not pass a specific parameter. The partial just displays the parameter value.

<%= passed_parameter %>

If an instance variable is used, the <%= … %> will result in an empty string if the instance variable (@abcdef) id not defined. But if its a local variable like the one passed to the partials using LOCALS, it will throw an error.

In these cases, the best thing to do is, add this at the first line of the PARTIAL.

<% passed_parameter ||= DEFAULT_VALUE %>

This has to be done for whatever optional parameters might be used.

Permalink Leave a Comment