Case statements and ERB

F

Farrel Lifson

Hi Rubyists,

While writing a template with ERB I found that case statements do not
seem to work:

irb(main):001:0> template =<<EOS
irb(main):002:0" <% case number %>
irb(main):003:0" <% when 1 %>
irb(main):004:0" <%= "one" %>
irb(main):005:0" <% when 2 %>
irb(main):006:0" <%= "two" %>
irb(main):007:0" <% else %>
irb(main):008:0" <% "more than two" %>
irb(main):009:0" <% end %>
irb(main):010:0" EOS
=> said:
\n<% else %>\n<% \"more than two\" %>\n<% end %>\n"
irb(main):011:0> require 'erb'
=> true
irb(main):012:0> erbRunner = ERB.new(template)
=> #<ERB:0x2c497f8 @filename=nil, @src="_erbout = ''; case number ; _erbout.con
cat \"\\n\"\n when 1 ; _erbout.concat \"\\n\"\n_erbout.concat(( \"one\" ).to_s);
_erbout.concat \"\\n\"\n when 2 ; _erbout.concat \"\\n\"\n_erbout.concat(( \"tw
o\" ).to_s); _erbout.concat \"\\n\"\n else ; _erbout.concat \"\\n\"\n \"more th
an two\" ; _erbout.concat \"\\n\"\n end ; _erbout.concat \"\\n\"\n_erbout", @saf
e_level=nil>
irb(main):013:0> erbRunner.run(binding)
SyntaxError: compile error
(erb):1: syntax error
_erbout = ''; case number ; _erbout.concat "\n"
^
(erb):2: syntax error
when 1 ; _erbout.concat "\n"
^
from (erb):2
from (irb):13

(in this example no object number is defined but "more than two"
should be printed out)

Are case statements not allowed in ERB or should they be formatted in
a specific way to get them to work?

Thanks,
Farrel
 
J

Jamis Buck

Hi Rubyists,

While writing a template with ERB I found that case statements do not
seem to work:

irb(main):001:0> template =<<EOS
irb(main):002:0" <% case number %>
irb(main):003:0" <% when 1 %>
irb(main):004:0" <%= "one" %>
irb(main):005:0" <% when 2 %>
irb(main):006:0" <%= "two" %>
irb(main):007:0" <% else %>
irb(main):008:0" <% "more than two" %>
irb(main):009:0" <% end %>
irb(main):010:0" EOS

irb(main):011:0> require 'erb'
=> true
irb(main):012:0> erbRunner = ERB.new(template)
=> #<ERB:0x2c497f8 @filename=nil, @src="_erbout = ''; case number ;
_erbout.con
cat \"\\n\"\n when 1 ; _erbout.concat \"\\n\"\n_erbout.concat((
\"one\" ).to_s);
_erbout.concat \"\\n\"\n when 2 ; _erbout.concat
\"\\n\"\n_erbout.concat(( \"tw
o\" ).to_s); _erbout.concat \"\\n\"\n else ; _erbout.concat \"\\n\"\n
\"more th
an two\" ; _erbout.concat \"\\n\"\n end ; _erbout.concat
\"\\n\"\n_erbout", @saf
e_level=nil>

The problem is the way ERB compiles the strings. It basically tries to
do the following with your snippet:

case number
_erbout.concat "\n"
when 1
_erbout.concat "\n"

and so forth, which is not valid Ruby syntax. Try the following instead:

<% case number
when 1 %>
one
<% when 2 %>
two
<% else %>
something else
<% end %>

- Jamis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top