Using Haml helpers that return strings

I like to use Haml's helpers, but over the years the situation has changed a bit. The old way was just to hook to the buffer. Here is what I have:

def confirmation_table(field)
  # Be certain that if the user is logged in, his/her email and name show
  if field.respond_to? :user
    haml_tag('tr') {
      haml_tag('th', 'Email:')
      haml_tag('td', field.user.email)
    }
    haml_tag('tr') {      
      haml_tag('th', 'Name:')
      haml_tag('td', field.user.full_name)
    }
  else
    haml_tag('tr') {
      haml_tag('th', 'User Information:')
      haml_tag('td', 'Not specified.')
    }
  end

  field.class.columns.collect{|col| col.name}.reject{|col| 
    col =~ /_at$/ || 
    col =~ /_on$/ ||
    col =~ /_id$/ ||
    col == 'id'}.each do |col|
    haml_tag('tr') {
      haml_tag('th', ActiveSupport::Inflector::humanize(col))
      haml_tag('td', typeize(field, col))
    }
  end
end

This, of course, can be obtained in my opinion in the same way as:

- confirmation_table(@f)

However, for me it makes sense (for me) to return the string. I do not see how it haml_captureprovides the same structuring ability. Any clues?

+5
source share
1 answer

Wrap the calls haml_tagin capture_haml:

def confirmation_table(field)
  capture_haml do
    if field.respond_to? :user
      haml_tag(:tr) do
        haml_tag('th.email', 'Email:')
        haml_tag('td.email', field.user.email)
      end
      # ...
    end
  end
end

They will be captured and returned capture_haml.

+6
source

All Articles