Underscore js - Uncaught SyntaxError: Unexpected token if

Underscore js - Uncaught SyntaxError: Unexpected token if

我用的是小胡子插值:

    _.templateSettings = {
        interpolate: /\{\{(.+?)\}\}/g
    };

对于这段代码,我收到错误:Uncaught SyntaxError: Unexpected token if

<div class="d-none flex-container tip-container bg-white mt-5" data-ajax-template-empty-tip>

        <div class="tip-header border border-bottom-0 d-flex flex-row p-2">

            <div class="align-self-center mr-2">
                <a href="./{{countryName}}" target="_blank">
                    <img class="d-block" alt="" src="/images/flags/{{countryId}}.png">
                </a>
            </div>


            {{if (typeof(cityName) !== "undefined") { }}
            <i class="fas fa-arrow-left align-self-center mr-2"></i>
            <div class="align-self-center mr-2">
                <a class="badge badge-primary" role="button" href="/city/{{cityName}}" target="_blank">
                    <span>{{cityName}}</span>
                </a>
            </div>
            {{}}}

        </div>

    </div>

没有 if 语句它工作得很好,但是有它就不行了,知道哪里出了问题吗?我在网上看了每一个问题都想不通,我什至删除了插值并使用默认的 <% 但它不会。

在 if 之前尝试一个 space,像这样:

{{ if (typeof(cityName) !== "undefined") { }}

我必须添加此设置才能使其正常工作:

   _.templateSettings = {
            evaluate: /\{\{(.+?)\}\}/g,
            interpolate: /\{\{=(.+?)\}\}/g,
            escape: /\{\{-(.+?)\}\}/g
        };