Nested ERB block : Embedded Ruby « Development « Ruby






Nested ERB block


require 'erb'
template = %q{
<% if problems.empty? %>
  it is true
<% else %>
  Here is else
  <% problems.each do |problem, line| %>
    * <%= problem %> on line <%= line %>
  <% end %>
<% end %>}.gsub(/^\s+/, '')

template = ERB.new(template, nil, '<>')

problems = [["Use of is_a? ", 23],
            ["eval() is dangerous", 44]]
template.run(binding)

 








Related examples in the same category

1.Embedded Ruby
2.Embedded in the string are a pair of template tags, <%= and %>.
3.ERB template tags
4.ERB tags in a string