Variables and Arrays

Variables:

You can create a variable and put a value into it using the following function:

{{ putValue 'variablename' value outputtotemplate? }}

example:

{{ putValue 'id' (Request.Body 'jsonpath' '$.id') true }}

(The outputtotemplate boolean will determine if the value is written out to the template as well as stored in memory, or just stored in memory.)

You can read a variable and use it in another function, or simply render it into the template using the following function:

{{ getValue 'variablename' }}

example: simply rendering out:

{{ getValue 'price' }}

example: using within another function:

{{ add (getValue 'price') this.tax '0.00' }}

Arrays:

You can append items to an array using the following function: (this is useful within a loop when you need to sum a list of numbers at the end)

{{ addToArray 'arrayname' value outputtotemplate?}}

example:

{{ addToArray 'totalweight' (multiply (this.quantity) (csv 'products' 'upc' 'this.upc' 'weight') '') false}}

(The outputtotemplate boolean will determine if the value is written out to the template as well as appended to the array, or just appended to the array.)

You would generally not render an array straight into the template - they would typically be used by another function or block statement that takes arrays, using the following function:

{{ getArray <arrayname> }}

example: passing into another function:

{{ sum (getArray 'totalweight') '0.00' }}

example: adding a new column to an in memory csv data store:

{{ addToArray 'newPet' '2000' false }} 
{{ addToArray 'newPet' 'dogs' false }} 
{{ addToArray 'newPet' 'Violet' false }} 
{{ addToArray 'newPet' 'sold' false }}

{{ csvAddRow 'pets' (getArray 'newPet') }}

Last updated