質問

i have the following json

         var tt =   projects: [
            {
                "name": "A",
                "amount": "10"
            }, {
                "name": "B",
                "amount": "20"
            }, {
                "name": "C",
                "amount": "30"
            }
        ]

with dust.js template engine, is there a way to use this json and filter through to only render project with the name B?

i thought a dust helper {@if cond="{projects.name} == 'B' "}... could do it but i cannot make it work?

anyone has any ideas? any jsfiddle would be highly appreciated.

役に立ちましたか?

解決

This is possible using the @eq helper:

{#projects}
    {@eq key=name value="B"}
        My name is {name} and my amount is {amount}.
    {/eq}
{/projects}

This template will loop through each of the project items, but it will only print out:

My name is B and my amount is 20.

For this to work, you must have both dustjs and dustjs-helpers available.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top