2

Can I use a Coffeescript switch block in the eco templating engine? I tried a couple of variations, but I keep getting unexpected dedent errors.

Update: To appease the downvoters, here is what I expected to work

<% switch x : %>
<% when 1 : %>
    one
<% end %>
<% when 2 : %>
    two
<% end %>
<% end %>

But I get "Parse error on line 5: unexpected dedent"

Thilo
  • 257,207
  • 101
  • 511
  • 656

2 Answers2

2

ECO templates don't appear to support the switch statement.

The generated CoffeeScript code for your code is:

switch x
  __out.push '\n'
  when 1
    __out.push '\n    one\n'
  __out.push '\n'
  when 2
    __out.push '\n    two\n'
  __out.push '\n'

The two __out.push '\n' lines after switch x and the end of the second when statement don't seem to allow this CoffeeScript snippet to compile into JavaScript.

Looking through the code, I couldn't figure out how to prevent those lines from being printed. This might be a good bug to report to the guys that make eco.

Sandro
  • 4,761
  • 1
  • 34
  • 41
  • It seems that eco currently does indeed not support `switch`. I file a bug report. https://github.com/sstephenson/eco/issues/38 – Thilo Jan 29 '12 at 01:04
0

I'm only somewhat familiar with eco, but it seems as though it would just not be creating the right CS from that expression. Considering CS uses when x then y, I'm not sure you're getting that on compile.

You could try this instead:

<% switch x : %>
    <% when 1 then: %>
        one
    <% end %>
    <% when 2 then: %>
        two
    <% end %>
<% end %>
Nick Jurista
  • 1,423
  • 12
  • 15