Summary:
a) Is there a way to ask ERB to trim leading whitespace between the
start-of-line and a '<%'? (The equivalent of .gsub( /^\s+<%/, '<%' ) If
not, I suggest that one should exist.
b) What is up with the crazy syntax for the third param to ERB? Could
we perhaps come up with a cleaner, less-ambiguous way to specify
various parsing options? Hash options? One char per flag?
Details:
See the templates below. I want to output a certain text string
indented by a specific number of tabs, with a single line break at the
end. ERB does not seem to have a setting to ignore leading whitespace
for an opening <% tag. This means that I have to either manage the
whitespace manually (using _erbout) or play with where I put the <% and
%> tags (making the code surprisingly hard to read).
require 'erb'
desired_but_unusable_template = <<ENDTEMPLATE
Hello
<%
4.times{
%>
BLING
<%
}
%>
World
ENDTEMPLATE
ERB.new( desired_but_unusable_template ).run
#=> Hello
#=>
#=> BLING
#=>
#=> BLING
#=>
#=> BLING
#=>
#=> BLING
#=>
#=> World
ERB.new( desired_but_unusable_template, nil, '%>' ).run
#=> Hello
#=> BLING
#=> BLING
#=> BLING
#=> BLING
#=> World
gross_but_correct_template_1 = <<ENDTEMPLATE
Hello
<%
4.times{
%>
BLING
<%
}
%>
World
ENDTEMPLATE
ERB.new( gross_but_correct_template_1, nil, '%>' ).run
#=> Hello
#=> BLING
#=> BLING
#=> BLING
#=> BLING
#=> World
gross_but_correct_template_2 = <<ENDTEMPLATE
Hello<%
4.times{
%>
BLING<%
}
%>
World
ENDTEMPLATE
ERB.new( gross_but_correct_template_2 ).run
#=> Hello
#=> BLING
#=> BLING
#=> BLING
#=> BLING
#=> World
gross_but_correct_template_3 = <<ENDTEMPLATE
Hello
<%
4.times{
_erbout << "\t\t"
%>BLING<%
_erbout << "\n"
}
%>
World
ENDTEMPLATE
ERB.new( gross_but_correct_template_3, nil, '%>' ).run
#=> Hello
#=> BLING
#=> BLING
#=> BLING
#=> BLING
#=> World