#492 √ wontfix
Alex

content_tag should accept block using variable

Reported by Alex | June 26th, 2008 @ 02:34 PM

When using content_tag inside of a helper method, there is not currently a nice way of returning strings that contain more than one variable.

Examples:

content_tag(:p) do
string_one = fetch_value
string_two = fetch_value(2)
string_three = another_value
string_one + string_two + string_three
# or even [string_one, string_two, string_three].join
end

Code could be cleaner if you used the following approach, which I believe is also used in form_for.

content_tag(:p) do |content|
content << fetch_value
content << fetch_value(2)
content << another_value
end

Comments and changes to this ticket

  • wildchild

    wildchild June 26th, 2008 @ 03:08 PM

    Nice proposal! Where is a patch?

  • Alex

    Alex June 27th, 2008 @ 04:33 AM

    I would have submitted one but I'm not sure how to do it without breaking compatibility with the standard use in the view.

  • Clemens Kofler

    Clemens Kofler June 30th, 2008 @ 09:15 PM

    I don't like this idea at all. If you feel that you have to do something complex like modifying the output multiple times, you should consider refactoring it out into a helper method.

  • Glenn Powell

    Glenn Powell August 10th, 2008 @ 09:04 AM

    Clemens, I don't really understand what the problem is with this proposal. I don't see why this:

    
    content_tag(:p) do |content|
      content << fetch_value
      content << fetch_value(2)
      content << another_value
    end
    

    would be any worse than what you have to do now which is:

    
    content_tag(:p) do
      content = ""
      content << fetch_value
      content << fetch_value(2)
      content << another_value
      content
    end
    

    And I believe the point was that this whole call should in fact be in a helper method, as stated in the original post, but that it would be nicer to have the content variable passed to the content_tag block.

  • Clemens Kofler

    Clemens Kofler August 10th, 2008 @ 01:24 PM

    Glenn, I see your point but I still think the only way to keep it really clean is a helper method - and while I do care about style in the helper, I don't care that much that the two examples you gave would really make a difference for me. I didn't like the whole "view helpers now accept blocks" thing in the first place because IMO it clutters up the view (like when you use the block style with link_to), so I may be biased here ...

  • Tom Lea

    Tom Lea August 13th, 2008 @ 02:20 PM

    • → Tag changed from “content_tag enhancement helper view” to “content_tag enhancement helper patch view”

    What about...

    
    content_tag(:p) do
      returning("") do |content|
        content << fetch_value
        content << fetch_value(2)
        content << another_value
      end
    end
    

    ...as a middle ground?

    Either way, I've attached a patch to do what you describe... but I don't think it's quite what's needed yet.

    Issue:

    content += "fish" will simply loose the initial object reference. This would be a bit of a suprise for some users.

    Fix:

    Use a subclass of String that aliases += to << or similar.

    Enhancement:

    Allow something as follows:

    
    content_tag(:div) do |div|
      my_text.each_line do |line|
        div.content_tag(:p, line)
      end
    end
    

    but if we allow this, we would also want to allow:

    
    content_tag(:div) do |div|
      div.form_for(:magic) do |f|
        ...
      end
    end
    

    So we would end up having to proxy calls to all helpers... eugh!

  • DHH

    DHH November 16th, 2008 @ 09:06 PM

    • → State changed from “new” to “wontfix”

    I think this is probably too clever for its own good when it has to work in the view too.

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 »

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins