#196 √ invalid
Nick Dufresne

local variable values from method_missing in partials issue

Reported by Nick Dufresne | March 9th, 2008 @ 12:40 AM | in 0.9.4

I wasn't sure whether this should be a part of ticket #42 or not.

i get the following issue using local variables in partials. I don't believe this is the expected outcome. If i call partial("partial", :foo => "bar") then:


# _partial.html.erb

<% foo ||= 'default' %>
<%= foo %>

#outputs default regardless of whether foo is set or not

I suppose this has to do with the fact that value of foo comes from a method of the template, and not an actual local variable. So maybe if we moved to locals[:foo] this behavior would be easier to deal with.

Comments and changes to this ticket

  • Nick Dufresne

    Nick Dufresne March 10th, 2008 @ 08:22 PM

    • → Title changed from “local variables in partials issue” to “local variable values from method_missing in partials issue”

    I thought about this some more and I realize that the trouble with having local values retrieved through method_missing in template.rb is that we have no way of determining if the local value has been defined or not.

    defined?(foo) returns false because foo is not defined, even if we pass foo in as a local to a partial. The best ways i could think of to get around this issue is to either

    
    # will copy value of foo into foo_local
    <% foo_local = foo rescue nil %> 
    <% foo_local ||= 'default' %>
    
    # check @_merb_partial_locals directly ... obviously a bad idea
    <% foo_local = @_merb_partial_locals.key?(:foo) ? foo : 'default' %>
    
    

    i think the second makes a case for locals hash available to partials. That way we could use locals[:foo] like @_merb_partial_locals. There needs to be a standard way to check for values passed into partials.

  • Michael Klishin (antares)

    Michael Klishin (antares) May 11th, 2008 @ 03:13 PM

    • → Milestone changed from “” to “0.9.4”

    This ticket is 2 months old. Can you still reproduce the problem 0.9.3 or head? Thank you.

  • Nick Dufresne

    Nick Dufresne May 12th, 2008 @ 08:06 PM

    I just checked with the latest from git and the problem still exists for the following situation:

    
    <% foo ||= "default" %> 
    <%= foo %>
    
    

    outputs "default" regardless of whether :foo => "bar" or not is passed to partial method.

  • Michael Klishin (antares)

    Michael Klishin (antares) May 12th, 2008 @ 08:05 PM

    • → State changed from “new” to “open”
  • Michael Klishin (antares)

    Michael Klishin (antares) May 17th, 2008 @ 03:00 PM

    • → Assigned user changed from “” to “Michael Klishin (antares)”
    • → State changed from “open” to “invalid”

    This is (if you think about it) is a duplicate of #42, so please follow it. Thanks for example, it will be used for testing once decision is made on how locals should work.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket