I'm having some issues with blocks and capture in erb templates, and
the particular problem shows itself in that the expected output in the
template view is repeated due to something related to how capture
works. (Using Rails 3.x)


I have the following view helper:

module ColumnLayoutHelper

  def layout_columns(options = {}, &content_block)
    raise ArgumentError, "No block given" unless block_given?

    layout = ColumnLayoutBuilder.new
    layout.configure(options)
    content = capture(layout, &content_block)
    content_tag(:div, content, layout.main_wrapping, false)

  end

end

class ColumnLayoutBuilder < Object

  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::CaptureHelper

  attr_accessor :output_buffer, :column_counter
  attr_accessor :main_wrapping
  attr_accessor :column_wrapping

  def configure(options)
    @main_wrapping = options.delete(:main_wrapping)
    @column_wrapping = options.delete(:column_wrapping)
  end

  def column(&block)

    raise ArgumentError, "No block given" unless block_given?

    @column_counter ||= 0
    @column_counter = @column_counter + 1

    column_html_options = (column_wrapping ? column_wrapping.dup : {})
|| {}
    column_html_options.default = ""
    column_html_options[:class] = "column_#{column_counter} " +
column_html_options[:class]
    column_content = content_tag(:div, capture(self, &block),
column_html_options,false)
    column_content
  end

end

--

Now if I use this column builder helper with the following erb
template:

<%= layout_columns do |layout|  %>
<%= layout.column do %>
content_01
<% end %>
<%= layout.column do %>
content02
<% end %>
<% end %>


I get the following output.

<div>
  content_01
  <div class="column_1 ">content_01</div>
  content02
  <div class="column_2 ">
    content_01
    <div class="column_1 ">content_01</div>
    content02
  </div>
</div>


As you can see, the buffer in between the captures doesn't seem to be
cleared in the separate block scopes. As I sort of expected how
capture would work...

I guess I'm having the same issues as pointed out here
http://stackoverflow.com/questions/4352167/cant-suppress-output-in-nested-block-helper-in-rails-3

Any ideas on how to fix this?  Thanks

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to