Thilo
Thilo

Reputation: 262734

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

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"

Upvotes: 2

Views: 1158

Answers (2)

Sandro
Sandro

Reputation: 4771

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.

Upvotes: 2

Nick Jurista
Nick Jurista

Reputation: 1434

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

Upvotes: 0

Related Questions