如何在Dust模板中运行或调整

Roh*_*ary 3 dust.js dust-helpers

我使用的 "dustjs-helpers": "1.6.0",有"dustjs-linkedin": "^2.6.0".

在我的模板中,我需要检查OR条件

if( cherry === true || berry === true)
  path/to/template1/
else 
  path/to/template2/
Run Code Online (Sandbox Code Playgroud)

如何使用Dust帮助器实现这一目标?

Int*_*ang 7

因为您正在测试两个不同的变量,所以您需要两个不同的真值测试.

您可以将此逻辑放入模板中,也可以放入小帮助程序中.我会告诉你两种方式.

我们假设您的上下文如下所示:

{
  "cherry": false,
  "berry": true
}
Run Code Online (Sandbox Code Playgroud)

模板方法

这种方法需要dustjs-helpers> = 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}
Run Code Online (Sandbox Code Playgroud)

您必须在第二个真实测试中手动覆盖keyto berry.

不太棒的模板方法

适用于所有版本的dustjs-helpers.

{@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}
Run Code Online (Sandbox Code Playgroud)

缺点:这不是规模,它是丑陋的,它重复数据.

辅助方法

完全不需要粉尘助手.

{
  "cherry": false,
  "berry": true,
  "isFruit": function(chunk, context) {
    return context.get("cherry") || context.get("berry");
  }
}

{?isFruit}
  path/to/template1
{:else}
  path/to/template2
{/isFruit}
Run Code Online (Sandbox Code Playgroud)

优点:您可以在不更改模板的情况下添加更多条件.