gpt4 book ai didi

dust.js - 如何在 Dust 模板中运行或调节

转载 作者:行者123 更新时间:2023-12-01 09:55:23 26 4
gpt4 key购买 nike

我正在使用"dustjs-helpers": "1.6.0", 与 "dustjs-linkedin": "^2.6.0"

在我的模板中,我需要检查一个 OR 条件,例如

if( cherry === true || berry === true)
path/to/template1/
else
path/to/template2/

如何使用 Dust 助手完成此操作?

最佳答案

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

您可以将此逻辑放入您的模板中,或放入一个小助手中。我会告诉你这两种方法。

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

{
"cherry": false,
"berry": true
}

模板方法

此方法需要 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}

您必须在第二个真值测试中手动将 key 覆盖为 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}

缺点:这不能扩展,它很丑,它会重复数据。

辅助方法

根本不需要 dustjs-helpers。

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

{?isFruit}
path/to/template1
{:else}
path/to/template2
{/isFruit}

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

关于dust.js - 如何在 Dust 模板中运行或调节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29140924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com