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