0

Is there a way to select certain elements in a JSON view based on an sub-element's string value using Linked In's version of dust.js? For example, if I had this JSON view:

{
    people: [ 
        { name: "Fred", type: "a" },
        { name: "Harry", type: "a" },
        { name: "Linda", type: "a" },
        { name: "Mary", type: "b" },
        { name: "Bob", type: "b" },
        { name: "Joey", type: "b" }
    ]
}

and I wanted this output:


Fred
Harry
Linda

how would the template differ from this simple one?

{#people}
    <br/>{name}
{/people}

This would be executed in a browser.

4

1 回答 1

0

有一种方法可以做到这一点,但你需要包括灰尘助手

{#people}
  {@eq key=type value="a"}
    {name}
  {/eq}
{/people}

需要注意的几点:

  1. 我们正在使用 @eq 助手。不要使用 @if 助手,即使它可用(它存在安全和性能问题)。
  2. 在助手内部引用值时,您不需要将其包装在{ }. 在此示例中,我们使用key=type, 而不是key={type}or key="{type}"
于 2013-03-13T16:37:11.430 回答