Templates
Templates allow you to define the generic document structure (such as a welcome email message or a quote PDF), which is then converted into an actual document based on the provided data.
If you wish to learn more about a specific topic, refer to the subsections under
.Creating a Template
Templates are created and managed in the Corteza Admin.
Navigate to the
and click on the New button in the top-right corner.Enter the basic information; short name, handle, description, and template type. The template type determines the template format and implicitly entails what document types the template can render to.
A partial template is used as part of another template (for example, a generic header or a footer) and can not be rendered independently. You can convert the template from and to a partial template.
Click on the Submit button to prepare your template.
After you submit the base parameters, three new sections will appear.
The toolbox provides snippets for the most common operations, HTML template sample, and partial inclusion samples. |
|||
The template content editor provides a simple code editor to edit your template. The HTML template editor implements syntax highlighting and some other useful tools such as auto completion. |
|||
The preview section allows you to test how your templates will look when rendered into an actual document.
|
The Content
Advanced users are welcome to check out the plain text template full reference, the html template full reference, and the extended function docs. |
Let’s start by copying the default HTML sample from the toolbox.
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Title</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
If you don’t need any dynamic content (different name for different contacts), you can stop after this step. The above template is valid and can already be used.
In case you need dynamic content, more topics need to be covered.
Value Interpolation
Value interpolation allows you to define some placeholder that is then replaced with an actual value when the template is rendered.
In this case, this placeholder looks like this:
{{.name}}
The value replaces the above placeholder under the name
property from the provided value
object.
Let’s look at some examples.
Each example first defines the value
object and then the placeholder.
{
"name": "Jane"
}
{{.name}}
{
"contact": {
"details": {
"name": "Jane"
}
}
}
{{.contact.details.name}}
A complete example would look like this:
{
"contact": {
"details": {
"firstName": "Jane",
"lastName": "Doe"
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Title</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
</head>
<body>
<h1>Hello, {{.contact.details.firstName}} {{.contact.details.lastName}}!</h1>
</body>
</html>
Conditional Rendering
Conditional rendering allows you to show or hide sections of the rendered document based on the input parameters.
if
statement:{{if condition}}
The condition was true.
{{end}}
if-else
statement:{{if condition}}
The condition was true.
{{else}}
The condition was false.
{{end}}
if-else if-else
statement:{{if condition}}
The condition was true.
{{else if condition}}
The other condition was true.
{{else}}
Neither conditions were true.
{{end}}
The condition
part is an expression that returns a single boolean value.
An example of an expression:
{{if .lead.cost > 1000}}
The lead {{.lead.name}} was expensive!
{{end}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Handling Lists
Our templates make it quite easy to work with lists. For example, you would like to generate a quote with lots of line items.
The syntax for iterating over a list looks like this:
{{range .listOfItems}}
{{.itemName}}; {{.itemCost}}$
{{end}}
If you prefer to specify what variable the current item is stored into, use this syntax:
{{range $index, $item := .ListOfItems}}
{{$item.itemName}}; {{$item.itemCost}}$
{{end}}
Using Functions
Sometimes you need to process the data further before it is rendered to the document.
Some lighter processing can be handled directly by the template engine. More complex processing should be handled by the code that is requesting to render the template.
{{functionName arg1 arg2 ... argN}}
The passed argument can be a constant or a property from the provided data.
You can also chain functions. When two functions are chained, the left function’s output is passed into the argument of the right function.
{{funcA | funcB | ... | funcN}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Using Partials
Partials allow you to keep your documents consistent by using common headers and footers. Partials can also be useful when displaying Corteza’s resources, such as displaying a record in a table.
{{template "partial_handle"}}
The partial_handle
is the handle you used when you defined the partial.
For example:
{{template "email_general_header"}}
If your partial needs to access some data that you provided to the current template (the one that uses the partial), you need to provide the second argument to the partial inclusion process.
{{template "email_general_header" .property.to.pass}}
See the example below:
{
"contact": {
"values": {...}
},
"account": {
"values": {...}
}
}
If you want to pass the contact
to the partial, you should include your partial like shown below:
{{template "partial_handle" .contact}}
If you want to pass all of the data to your partial, you should include your partial like shown below:
{{template "partial_handle" .}}
You would access the contact
in your partial like shown below:
{{/* In case of the first example */}}
Hello {{.values.FirstName}}
{{/* In case of the second example */}}
Hello {{.contact.values.LastName}} of the {{.account.values.Name}}
The Preview
The preview section at the bottom of the page allows you to check how your documents will look like once the template is rendered.
The input box should contain a valid JSON object (render payload) with two root properties; variables
and options
:
{
"variables": {},(1)
"options": {}(2)
}
1 | The variables parameter defines what data will be available one the document is rendered.
The structure is not defined. |
2 | The options parameter defines the rendering options and is currently only available for PDFs. |
{
"variables": {
"param1": "value1",
"param2": {
"nestedParam1": "value2"
}
},
"options": {
"documentSize": "A4",
"contentScale": "1",
"orientation": "portrait",
"margin": "0.3"
}
}
A
The options marked with PDF only can only be used with PDF documents and are ignored elsewhere. |
|
|||
PDF only.
|
|||
PDF only.
|
|||
PDF only.
|
|||
PDF only.
|
|||
PDF only.
|
|||
PDF only.
|
|||
PDF only.
|
|||
PDF only.
|
|||
PDF only.
|
|||
PDF only.
|
|||
PDF only.
|
|||
PDF only.
|