我正在使用"dustjs-helpers": "1.6.0"和"dustjs-linkedin": "^2.6.0"。
在我的模板中,我需要检查一个或条件,如
if( cherry === true || berry === true)
path/to/template1/
else
path/to/template2/我怎样才能用沙尘帮手来完成这个任务呢?
发布于 2015-03-19 17:22:34
因为你测试的是两个不同的变量,所以你需要两个不同的真实测试。
您可以将此逻辑放入模板中,也可以放入小助手中。我会教你两种方式。
假设您的上下文如下所示:
{
"cherry": false,
"berry": true
}模板法
此方法需要>= 1.6.2
您必须包括两个{@eq}检查。因为您正在使用这样一个最新版本的Dust,所以您可以访问{@any}和{@none}帮助程序。
{@select key=cherry}
{@eq value="true" type="boolean"/}
{@eq key=berry value="true" type="boolean"/}
{@any}path/to/template1{/any}
{@none}path/to/template2{/none}
{/select}在第二次真相测试中,您必须手动将key重写为berry。
较少的模板方法
适用于所有版本的清洁工-帮手。
{@eq key=cherry value="true" type="boolean"}
path/to/template1
{:else}
{@eq key=berry value="true" type="boolean"}
path/to/template1
{:else}
path/to/template2
{/eq}
{/eq}缺点:它不缩放,它很丑,它重复数据。
辅助法
根本不需要清洁工。
{
"cherry": false,
"berry": true,
"isFruit": function(chunk, context) {
return context.get("cherry") || context.get("berry");
}
}
{?isFruit}
path/to/template1
{:else}
path/to/template2
{/isFruit}优点:您可以在不更改模板的情况下添加更多条件。
https://stackoverflow.com/questions/29140924
复制相似问题