Apache Templates in Snaps

Certain Snaps, like JSON Generator, let you use Apache Velocity Templates when you edit the JSON, XML, or Envelope.

For example, when you click Edit JSON in the JSON Generator Snap, you see the following:

## Enter your JSON-encoded data in this space.  Note that this text is
## treated as an Apache Velocity template, so you can substitute values
## from input documents or the pipeline parameters.  See the following
## URL for more information about Velocity:
##   https://velocity.apache.org/engine/devel/user-guide.html

[
    {
##        "msg" : "Hello, World", "num" : 1
    }
]


## Tips:
##  * The sample data above will generate a single empty document, uncomment
##    the line in the middle to include the sample fields.  Adding more
##    objects to the root array will cause the Snap to generate more
##    than one document.
##  * Pipeline parameters can be referenced by prefixing the parameter
##    name with an underscore, like so:
##      ${_pipelineParamName}
##  * If you add an input view to the Snap, this template will be
##    evaluated for each input document.
##  * Fields in the input documents can be referenced by prefixing them
##    with a dollar-sign ($), like so:
##      $parent.child[0].value
##  * Any referenced document values and pipeline parameters will
##    automatically be JSON-encoded when they are inserted into the final
##    JSON document.  You should not have to worry about escaping values
##    yourself.
##  * If you are having troubles getting a template to produce valid JSON,
##    you can add an error view to the Snap to get a document that
##    contains the output of the template evaluation.

Examples

JSON

{
    "books": [
        {
            "msg": "Hello, World",
            "num": 1
        },
        {
            "msg": "Hello, World2",
            "num": 2
        }
    ]
}

linked to xml generator:
<?xml version="1.0"?>
<books>
#foreach( $book in $books )   
    <book id="$book.num">
        <title>$book.msg</title>
    </book>
#end
</books>

SOAP

<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<obtainOrderSrps
xmlns="http://ws.licmgr.eos.ecomm.rim.com/">
<arg0
xmlns="">
<countryCode>$countryCode</countryCode>
<customerId>$customerId</customerId>
#foreach($item in $items)

<items>
<lineItem>$item.lineItem</lineItem>
<qty>$item.qty</qty>
<skuCode>$item.skuCode</skuCode>
</items>
#end

<orderNumber>$orderNumber</orderNumber>
<eCommOrderNumber>$eCommOrderNumber</eCommOrderNumber>
</arg0>
</obtainOrderSrps>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

See Also