From: Daniel Berger Date: 2007-03-08T02:16:28+09:00 Subject: Re: Trimming whitespace from ERB On Sep 27 2005, 7:55 am, "Phrogz" wrote: > 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. I second this. > 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? And this one, too. Anyone got a patch? > 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 = < 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 = < 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 = < 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 = < 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 Regards, Dan