Can I use a Coffeescript `switch` block in eco?

1.1k Views Asked by At

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"

2

There are 2 best solutions below

1
On BEST ANSWER

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.

0
On

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 %>