让我们研究以下代码:
terminalsListHtml += this.compiled(_.extend(this.terminals[i], {clazz: 'all'}, obj));
模板:
compiled: _.template($('#terminal-template').text()),
以下选择器
$('#terminal-template').text()
返回
"
<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>
"
以下代码行
_.extend(this.terminals[i], {clazz: 'all'}, obj)
但执行后
terminalsListHtml += this.compiled(_.extend(this.terminals[i], {clazz: 'all'}, obj));
terminalsListHtml
等于
"
<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>
"
我错了什么?
答案 0 :(得分:0)
Underscore.js模板使用EJS样式模板,而您使用其他类型。您可以更改模板设置以适用于您的模板,如下所示:
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
};
var template = _.template("Hello {{ name }}!");
template({name: "Mustache"});
=> "Hello Mustache!"
这是从documentation复制的。