6

I need to be able to format unprinted logic lines in ERB without affecting the eventual text output of the template. At this point, I don't think that ERB supports such escaping.

This is my first major Ruby project. I am writing a code generator. My templates will include a considerable number of conditionals and enumerations. To make the template readable and maintainable, I need to be able to format the logical code and comments without distorting the eventual output.

E.g. Suppose I want this output:

Starting erbOutput
1
2
3
4
Ending erbOutput

I naively wrote the template something like so:

require 'erb'
h=<<H
Starting erbOutput
<%# comment %>
<%5.times do |e|%>
<%=e.to_s  %>
<%end %>
<%# comment %>
Ending erbOutput
H
s=ERB.new(h).result
puts s

... but this produces

Starting erbOutput


0

1

2

3

4


Ending erbOutput

A straight print:

"Starting erbOutput\n\n\n0\n\n1\n\n2\n\n3\n\n4\n\n\nEnding erbOutput\n"

...makes it clear that the line-returns of the logic and comment lines are being included in the ERB output.

I can produce the desired output by cramming the template into this awkward form:

h=<<H
Starting erbOutput<%# comment %>
<%5.times do |e|%><%=e.to_s  %>
<%end %><%# comment %>Ending erbOutput
H

... but I don't think I can debug and maintain templates without more readable formatting. Some of my conditionals and enumerations are as much as three levels deep and I comment heavily. Cramming all that on one or two lines makes the template utterly unreadable.

Is there a way to escape or otherwise suppress the line returns of logic an comment lines in ERB? Does one of the other commonly available Ruby template modules handle this better?

In case it matters, I am working in MacRuby 0.10 (implements Ruby 1.9.2) on MacOS 10.6.7.

TechZen
  • 64,370
  • 15
  • 118
  • 145

4 Answers4

4

Minus sign?

<%# comment -%>
<% 5.times do |e| -%>
<%= e.to_s  -%>
<% end -%>
<%# comment -%>
fl00r
  • 82,987
  • 33
  • 217
  • 237
3

As Rom1 and Kyle suggest, you can pass parameters to ERB.new, but then, you will not get line breaks where you want.

require 'erb'
h=<<H
Starting erbOutput
<%# comment %>
<%5.times do |e|%>
<%=e.to_s  %>
<%end %>
<%# comment %>
Ending erbOutput
H
s=ERB.new(h, nil, '<>').result
puts s

gives you

Starting erbOutput
01234Ending erbOutput

So you need to explicitly insert extra lines

require 'erb'
h=<<H
Starting erbOutput
<%# comment %>
<%5.times do |e|%>
<%=e.to_s  %>

<%end %>
<%# comment %>
Ending erbOutput
H
s=ERB.new(h, nil, '<>').result
puts s

This will give:

Starting erbOutput
0
1
2
3
4
Ending erbOutput
sawa
  • 165,429
  • 45
  • 277
  • 381
  • And the answer was down in the ERB docs all along. Strange how hard it is to use docs in an language you are unfamiliar with. This works fine with the standard Ruby 1.8.7 but fails with a `LocalJumpError: no block given` under Macruby. I guess now I will have to figure out if this is a ruby 1.9.2 issue or (more likely) an macruby issue. – TechZen May 11 '11 at 22:13
  • Looks like it's a known Macruby issue now fixed: http://www.macruby.org/trac/ticket/1252 – TechZen May 11 '11 at 22:16
1

You can change the settings for erb. Here's a quick tutorial: http://www.ruby-forum.com/topic/55298

Kyle Sletten
  • 5,365
  • 2
  • 26
  • 39
0

erb -T 1 foo.erb

I suppose there is an equivalent option in the library (probably the trim_mode ctor param).

Rom1
  • 3,167
  • 2
  • 22
  • 39