I have some partials that have “optional” local variables. By default Middleman throws an “undefined local variable or method” error when you try to reference a local variable that hasn’t been defined. I came across http://stackoverflow.com/q/2060561/538400 which seems to offer a lot of hacks.
I have a particular partial that has a few of these “optional” local variables. It seems really messy to have a bunch of conditional statements checking if things are defined, etc. My thought was to build a helper that could test for the presence of a local variable and return nil if not.
module PartialHelpers
def local(name)
locals.has_key?(name) ? send(name) : nil
end
end
In the template I’d do something like this:
<% if local(:something) %>
<aside><%= something %></aside>
<% end %>
With this I get “undefined local variable or method ‘locals’”. How can I get this to work, or is there a better way to write such a helper?