action – template
A template can be evaluated against an individual record or, if the template itself contains control structures
that specify one or more records, can be evaluated without the need to specify a record.
Template on a Specific Record
Sample Request
{
"account": *** your account id ***,
"apikey": "*** your api key ***",
"action": "template",
"recordurl": "person/12345",
"template": "My name is {FullName}. My email address is <b>{Email}</b>"
}
Sample Response
{
"success": true,
"output": "My name is John Smith. My email address is <b>john@smithmotors.co.uk</b>"
}
Template without specifying a record
The following example uses built in display calculation functions to assist in building an HTML table list of records.
Note the use of the EACH control structure which evaluates the template within the EACH / END structure once for each
record, with the record being referenced in the variable rPerson.
All expressions in a template within curly braces are evaluated as calculations.
Sample Request
{
"account": *** your account id ***,
"apikey": "*** your api key ***",
"action": "template",
"template": "
<h3>People</h3>
{displayTableStart("FirstName", "LastName")}
EACH Person AS rPerson
{displayTableRow(rPerson:FirstName, rPerson:LastName)}
END
{displayTableEnd()}
"
}
Sample Response
{
"success": true,
"output": "
<h3>People</h3>
<table class="display">
<thead><tr><th>FirstName</th><th>LastName</th></tr></thead>
<tbody>
<tr><td>John</td><td>Smith</td></tr>
<tr><td>Rob</td><td>Smith</td></tr>
<tr><td>Darth</td><td>Vader</td></tr
</tbody></table>
"
}
Template with Data Provided in call
You can pass data into a template – the data values provided will override the data that might otherwise have been available in a record (if specified).
This means that you can use the full power of PYXI templating and calculations even without reference to any PYXI data records.
Sample Request
{
"account": *** your account id ***,
"apikey": "*** your api key ***",
"action": "template",
"recordurl": "person/12345",
"template": "My name is {FullName}, known as {NickName}. My email address is <b>{Email}</b>",
"data": {
"nickname": "Jonno”
}
}
Sample Response
{
"success": true,
"output": "My name is John Smith, known as Jonno. My email address is <b>john@smithmotors.co.uk</b>"
}
Multiple Templates
For any of the template calls discussed above, you can pass an array of template strings, each keyed with a name,
in template, and receive a corresponding output array e.g.
"template": {
"template1": "My name is {FullName}",
"template2": "My first name is {FirstName}"
}
Gives response
"output": {
"template1": "My name is John Smith",
"template2": "My first name is John"
}
This is useful if multiple template blocks are required for display together, allowing for a single API call rather than one call for each template.
Note also the template format option when requesting record values in e.g. Read a Record or List Records.