如何使用underscore.js模板if语句? [英] How to use if statements in underscore.js templates?
本文介绍了如何使用underscore.js模板if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的模板化underscore.js功能都做这样的一个模板:
I'm using the underscore.js templating function and have done a template like this:
<script type="text/template" id="gridItem">
<div class="griditem <%= gridType %> <%= gridSize %>">
<img src="<%= image %>" />
<div class="content">
<span class="subheading"><%= categoryName %></span>
<% if (date) { %><span class="date"><%= date %></span><% } %>
<h2><%= title %></h2>
</div>
</div>
</script>
正如你可以看到我在那里有一个if语句,因为我所有的车型将不会有日期参数。但是这样做的这种方式给了我日期没有定义的错误
。所以,我该怎么办,如果一个模板中的语句?
As you can see I have an if statement in there because all of my models won't have the date parameter. However this way of doing it gives me an error date is not defined
. So, how can I do if statements within a template?
推荐答案
这应该做的伎俩:
<% if (typeof(date) !== "undefined") { %>
<span class="date"><%= date %></span>
<% } %>
请记住,在underscore.js模板如果
和为
只是标准的JavaScript语法包装在&LT; %%方式&gt;
标签
Remember that in underscore.js templates if
and for
are just standard javascript syntax wrapped in <% %>
tags.
这篇关于如何使用underscore.js模板if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文