迭代数组文字

时间:2016-07-20 14:37:19

标签: dust.js

是否可以在灰尘中迭代数组文字?

如果我有这样的JSON {arr: ["a", "b", "c"]},我会这样做:

{#arr}
....
{/arr}

有没有办法用我在尘埃模板中定义的数组来做到这一点?也许是这样的事情:

{#["a", "b", "c"]}
{/?}

我知道这不是正确的语法,但希望它说明了我正在尝试做的事情。

1 个答案:

答案 0 :(得分:1)

Dust本身不包含此语言功能。这就是为什么你可以在你的上下文中编写帮助或作为全局函数向Dust添加新功能。

这是一个迭代项列表的基本帮助器。没有错误检查或任何事情,但这是一个起点。

> pip install freetype-py
> pip install pypng
> pip install matplotlib

然后,在模板中使用它:

function HashMaker {

        $enc = [System.Text.Encoding]::UTF8
        $hasher = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider

        return {
            param($value)
            [System.BitConverter].ToString($hasher.ComputeHash($enc.GetBytes($value)))
        }.GetNewClosure()
}

如果您在单个上下文中使用它,您也可以将帮助程序直接放入Dust上下文中,并像dust.helpers.iterate = function(chunk, context, bodies, params) { var items = params.items.split(','); return chunk.section(items, context, bodies, params); } 一样调用它们。

相关问题