Dust-motes

HELPERS / CONTROL

iterate

{@iterate key=obj sort=xxx /}

Iterate over a JavaScript object

Parameters:

  • key : object to iterate over
  • sort (optional): Optional parameter to select sorted output based on the keys. Value can be "asc" for ascending sort, "desc" for descending sort or any other string that defines a user-supplied comparison function for the sort.

Examples:


              Show $key, $value, an $type usage
              Data: { obj: {a:"A", b:"B", c:"C" } }

              {@iterate key=obj}
                {$key}:{$value} {$type} 
              {/iterate}

              Output: a:A string b:B string c:C string
          

            Ascending sort
              Data: { obj: {c:"C", a:"A", b:"B" } }

              {@iterate key=obj sort="asc"}
                {$key}:{$value}
              {/iterate}

              Output: a:A b:B c:C
          

              User-defined compare function for numeric sort. The function must be in the dust globals.
              var compareNumbers = function(a,b) {
                var aa= parseInt(a, 10); 
                var bb = parseInt(b, 10);
                return aa-bb;
              }

              Data: { obj: {10:"C", 1:"A", 300:"B" }  }

              {@iterate key=obj sort="compareNumbers"}
                {$key}:{$value}
              {/iterate}

              Output: 1:A 10:C 300:B 
          

if

{@if test=expr}...{/if}

If test expr and if true, evaluate body

Parameters:

The test="expr" expression is intentionally limited. So what is allowed is:

The variables are restricted to dust names and path used to access values from the context. Constants are JavaScript integer, float, hex and string forms ("xx" or 'xx'). Operands can be a "variable", a constant, or a binary or unary expression yielding a value. Relational operators are <, >, <=, >=, ==, !=. Boolean operators are ! (unary), || and &&.. Operator precedence is the same as JavaScript and parentheses are allowed for clarity or for when the precedence is not what you want.

Expressions are easier to write because names and paths do not have to be wrapped in braces or braces within quotes. For example, using the original if helper you write {@if cond="\'{state}\' == 'CA' "} versus {@if test="state == 'CA'} with this one.

Examples:


		Context:
		 { 
		    x:1,
                    y:2, 
                    a: {b: 5}, 
                    arr: [1, 2, 3], nums:[1, 2, 3]
                 }

               {@if test="x"}3{/if} is true and outputs 3 
               {@if test="a.b == 5"}5{/if} is true and outputs 5 
               {@if test="arr[nums[1]]==3"}3{/if} is true and outputs 3 
               {@if test="2>1 && 3>=3 && 4==4"}true{/if} outputs true 
               {@if test=" x && y "}X and Y exist{:else}X and Y do not exists/if} outputs "X and Y exist" 
               {#arr}{@if test=". < 4"}1{/if}{/arr} outputs 111 because all elements of arr are < 4 
               {@if test="!!0"}true{:else}false{/if} outputs false
          

Notes: