Trying to test for local variables in a partial but can't access the `locals` method in a helper

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?

Does anyone know why I can access locals from within a page template but not within a custom helper method? This is weird because self and self.object_id each output the same thing, respectively, when called from within the page template and in the helper method.