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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: