DustJS xpath next sibling axis equivalent

85 Views Asked by At

How do I use parameters in DustJS? I made a jsfiddle where I want to print key value pairs where the key = [parameter].

In the fiddle I just want to display Larry, Moe and Curly's weight (not their height).

In XSLT this is easy. Just use Xpath to find prop[@name="weight"] then the following-sibling axis.

Fiddle: http://jsfiddle.net/6YrCg/

<script id="entry-template">
{title}

<ul>
    {#names}
    <li>{name}</li>{~n}
    <ul><li>Weight:{#props.name}{value}{/props.name}</li></ul>
    {/names}
</ul>
</script>

<div id="output"></div>




$(document).ready(function () {
    var data = {
        "title": "Famous People", 
        "names" : [{ "name": "Larry", "props":[{"name":"height","value":"5.8"},{"name":"weight","value":"160"}] },{ "name": "Curly", "props":[{"name":"height","value":"5.9"},{"name":"weight","value":"200"}]},{ "name": "Moe", "props":[{"name":"height","value":"5.8"},{"name":"weight","value":"160"}]}]
    }

    var source   = $("#entry-template").html();
    var compiled = dust.compile(source, "intro");
    dust.loadSource(compiled);

    dust.render("intro", data, function(err, out) {
        $("#output").html(out);
    });
});
1

There are 1 best solutions below

2
On BEST ANSWER

Here is one solution, using the {@eq} helper to only output the value if the name key is equal to "weight".

{title}

<ul>
    {#names}
    <li>
      {name}
      <ul><li>Weight: {#props}{@eq key=name value="weight"}{value}{/eq}{/props}</li></ul>
    </li>
    {/names}
</ul>

Context helpers such as {@eq} are functions that augment the core syntax of Dust. For more information about this helper and other officially-supported helpers, check out the dustjs-helpers wiki page.