javascript doT.js:在 dot.js 中链接 if-else if
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21784346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
doT.js: chained if-else if in dot.js
提问by Manish Kumar
I was trying doT.js
template engine.How to do nested if-else if in dot.js
like
我正在尝试doT.js
模板引擎。如何嵌套 if-else if in dot.js
like
if()
.....
else if
.....
else if
.....
else
.....
回答by BenM
You can use the following syntax:
您可以使用以下语法:
{{? it.name }}
<div>Oh, I love your name, {{=it.name}}!</div>
{{?? it.age === 0}}
<div>Guess nobody named you yet!</div>
{{?? it.age > 20}}
<div>You're old!</div>
{{??}}
You are {{=it.age}} and still don't have a name?
{{?}}
The above gets compiled to:
以上被编译为:
function anonymous(it /**/)
{
var out='';
if(it.name)
{
out+='<div>Oh, I love your name, '+(it.name)+'!</div>';
}
else if(it.age === 0)
{
out+='<div>Guess nobody named you yet!</div>';
}
else if(it.age > 20)
{
out+='<div>You\'re old!</div>';
}
else
{
out+='You are '+(it.age)+' and still don\'t have a name?';
}
return out;
}
Essentially, just keep adding {{?? condition}}
until you get to {{?}}
(the end of the block).
本质上,只需继续添加{{?? condition}}
直到到达{{?}}
(块的末尾)。
回答by Agnaldo Junior
You can do it like this:
你可以这样做:
<!-- CONDITIONAL !-->
<!-- if -->
<div>
{{? it[0].firstName }}
{{=it[0].firstName }}
{{?}}
</div>
<!-- if / else if / else -->
<div>
<!-- if -->
{{? it[0].lastName }}
<span>First name is set</span>
<!-- else if -->
{{?? it[0].zip}}
<span>You don't have a last name</span>
<!-- else if -->
{{?? it[0].firstName}}
<span>Your firstName is: {{=it[0].firstName}}</span>
<!-- else if end -->
{{??}}
<!-- else -->
<span>You didn't set your name</span>
<!-- if end -->
{{?}}
</div>
回答by Agnaldo Junior
this image tag is incorrect:
此图片标签不正确:
<img src="{{=(user.absolutePath=='')?'img/user-shape.png':user.absolutePath;}}">
this is correct:
这是对的:
{{~it.userCollection[0].attributes.admin :user:index}}
<div class="team-content-user">
<!-- if not null-->
{{?user.absolutePath}}
<img src="{{=user.absolutePath}}">
<!-- if not (not null) -->
{{??!user.absolutePath}}
<img src="img/user-shape.png">
{{??}}
{{?}}
{{=user.email}}</br>
</div>
{{~}}