简体   繁体   中英

Underscore.js doesn't substitutes values

Lets research the following code line:

terminalsListHtml += this.compiled(_.extend(this.terminals[i], {clazz: 'all'}, obj));

template:

compiled: _.template($('#terminal-template').text()), 

the following selector

$('#terminal-template').text()

returns

"
    <li data-terminal-id="{{ id }}" class="{{ clazz2 }}">
        <label>
            <input type="radio" name="terminal" class="{{ clazz }}" data-terminal-id="{{ id }}"/>
            <a href="/admin/createCompany/getOriginalImage/{{imageId}}" title=""
               class="fancy_image small-picture"><img
                    src="/admin/createCompany/getMediumThumbnail/{{imageId}}"
                    alt=""/></a>

            <h3>{{ name }}
                <small>{{ place }}</small>
            </h3>
            <p>{{ description }}</p>

            <p class="count">Проходимость: <span>{{ count }}</span> чел./час</p>

            <p class="count">Стоимость: <span>{{ amount }}</span> руб./час</p>
        </label>
    </li>
"

the following code line

_.extend(this.terminals[i], {clazz: 'all'}, obj)

returns
在此处输入图片说明

But after execution

terminalsListHtml += this.compiled(_.extend(this.terminals[i], {clazz: 'all'}, obj));

terminalsListHtml equals

"
    <li data-terminal-id="{{ id }}" class="{{ clazz2 }}">
        <label>
            <input type="radio" name="terminal" class="{{ clazz }}" data-terminal-id="{{ id }}"/>
            <a href="/admin/createCompany/getOriginalImage/{{imageId}}" title=""
               class="fancy_image small-picture"><img
                    src="/admin/createCompany/getMediumThumbnail/{{imageId}}"
                    alt=""/></a>

            <h3>{{ name }}
                <small>{{ place }}</small>
            </h3>
            <p>{{ description }}</p>

            <p class="count">Проходимость: <span>{{ count }}</span> чел./час</p>

            <p class="count">Стоимость: <span>{{ amount }}</span> руб./час</p>
        </label>
    </li>
" 

what do I wrong?

Underscore.js templating uses EJS-style templates, while you use a different kind. You can change the templating settings to work for your templates like so:

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

var template = _.template("Hello {{ name }}!");
template({name: "Mustache"});
=> "Hello Mustache!"

This was copied from the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM