nevi_me
nevi_me

Reputation: 2730

Underscore template: error with if else statement

I have an error parsing an underscore template, I keep getting an error saying:

Uncaught SyntaxError: Unexpected token else

Here is the template below: using Jade

script#viewLinksTemplate(type="text/template")
    table.table.table-condensed.table-striped
        caption Available links
        thead
            tr
                td link id
                td Name
                td View
                td Edit
                td Remove
        tbody
            {[ _.each(routes, function(route) { ]}

            tr
                td {{ route._id }}
                {[ if (typeof route.name !== 'undefined') { ]}
                td {{ route.name }}
                {[ } ]}
                {[ else { ]}
                td {{ route.stations.start.name }} - {{ route.stations.end.name }}
                {[ }; ]}
                td
                    a.btn.btn-primary(href="/route") View Route
                td
                    a.btn.btn-warning Edit Route
                td
                    a.btn.btn-danger Delete Route
            {[ }); ]}

The problem seems to be in the if/else statement. When I remove it completely the parts that are left on the template are rendered.

So, am I missing something with the syntax? All my other templates have javascript, and render correctly, except this one with the if/else.

Thanks

Upvotes: 4

Views: 3553

Answers (1)

freejosh
freejosh

Reputation: 11383

It's because the end of the if statement and the else are in two different evaluate tags. Merge them into one by replacing:

{[ } ]}
{[ else { ]}

with:

{[ } else { ]}

Upvotes: 9

Related Questions