Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 918
nevi_me Avatar asked Jan 15 '23 17:01

nevi_me


1 Answers

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 { ]}
like image 91
freejosh Avatar answered May 01 '23 16:05

freejosh