Templates: reference
This page gives the rules of templates. Templates gives the procedures.
Key reference
| Key | In | Type | Meaning |
|---|---|---|---|
params | template | object | The params of the template, by name. A template sees only its params and parent. |
counter | template | object | { asd: n }: each use takes the next value of the counter asd as the param n. A block that contains the use must declare the counter. |
repeat | template | object | Repeat the body of the template. { count, index }. |
instances | template | array | The instances that each use adds. An - expand: entry here is a nested block. |
mapping | template | array | The mapping entries that each use adds, at the end of mapping:. |
initializers | template | array | The initializers that each use adds, at the end of initializers:. |
expand | template | ExpandBlock | A block that runs after the entries of the template. Use it to make children. |
parent | expand block | string | The parent of the entries. Templates read it as ${parent}. Without it, the block uses the parent of the enclosing block. |
with | expand block | object | Values that are the same for all iterations. The block gives them to each template that declares a param with that name. |
counters | expand block | object | The counters of the block, by name. A counter lives in this block and in the blocks in it. |
use | expand block | string | The template that each iteration uses. |
layout | expand block | array | The templates that each iteration uses, in order. An entry is a name, { use, repeat, with } or { expand }. |
repeat | expand block | number or string | The number of iterations. An expression can give it. |
index | expand block | TemplateIndex | The value of each iteration. n gives 0, 1, 2. A map gives a counter with a name: { name: n, start: 1, format: "000" }. |
over | expand block | array or object | One iteration for each record. A list of maps, or a CSV table { file }. |
reverse | expand block | boolean or string | The counters of the block give their values from the other end. The order of the entries does not change. |
instances | expand block | array | Instances that each iteration adds, without a template. |
mapping | expand block | array | Mapping entries that each iteration adds, without a template. |
initializers | expand block | array | Initializers that each iteration adds, without a template. |
start | counter | number | The first value. The default is 0. |
step | counter | number | The increase between two values. The default is 1. |
format | counter | string | A run of "0". The value becomes text, with zeros on the left to this width: "00" gives 01, 02. |
in | counter | array | The values, in order. Do not use it with start or step. |
type | param | "string", "number", "boolean" | The type of the value. A value of another type is an error. |
default | param | any | The value when the block gives no value. Without a default, the param is required. |
enum | param | array | The only values that the param accepts. |
inherit | param | boolean | When the block gives no value, take the value of the same name from the enclosing template call. |
file | table (over:) | string | The CSV table, relative to the configuration file. Its first line names the columns. |
delimiter | table (over:) | ",", ";" | The separator of the cells. Without it, the expander finds it from the first line. |
decimalSeparator | table (over:) | ".", "," | The decimal separator of the numbers. The default is ".". The expander never guesses it. |
Where each value comes from
- A template sees only its params and
parent. Another name is an error (TPL-E002). - The block gives the params: its
with:, the current record and thewith:of the layout entry. Thewith:of the layout entry has priority. - Do not give the same name in the
with:of the block and in a record (TPL-E004). - A param with
inherit: truegets the value of the same name from the enclosing template call when the block gives no value. - A param without
inherit: truenever gets a value from the enclosing call. - The order is: the value from the block, then the inherited value, then the default. If there is no value, the expander reports TPL-E003.
Expressions
The text in ${…} is a jsonata expression. Binding formulas use the same language.
- A field that is one expression only keeps the type of the value:
value: "${capacity}"gives the number 50. - A field with text around the expression gives text:
"DB${capacity}"gives"DB50". $fmt(value, "000")adds zeros on the left:$fmt(7, "000")gives"007".$now(),$millis(),$random()and$eval()are not allowed. An expansion must give the same result each time.$namewithout braces is not a template expression. In amapping:entry,$rawstays for the runtime.
What the gateway checks when it starts
- Each optional of a generated instance must be a member of its type. If it is not, the gateway shows one warning for the template line, with the number of instances. Without this check, node-opcua does not create the optional and shows nothing.
- When a generated instance cannot be created, the error names the template and the iteration:
(generated by instances[1]/Mill[2]).
Templates in VS Code and in the browser editor
The omni-edge language server (the VS Code extension, or opcua-omni-edge lsp) and the browser editor expand the templates while you type, with the same expander as the gateway.
- Completion gives the template names after
use:, in alayout:and in a module, the params of the used template inwith:and in anover:record, and the counter names incounter:. - Hover on
- expand:shows the number of entries that the block makes, and their first names. - Hover on a template name shows its params.
- Go to Definition on a template name opens its definition in
templates:. - A TPL error shows on the line of the template that causes it.
- The binding rules check the generated
mapping:entries too. An error on a generated entry shows on the line of its template, with the iteration:(generated by instances[1]/Sensor[2]).
In the browser editor, the TPL errors and the binding errors come with the other gateway checks, and the squiggle is on the first line of the template. The address-space panel shows the generated instances with a tpl mark. Its tooltip names the template and the iteration. Click a generated instance to select its template in the YAML.
Limits
- Templates do not read the address space or the NodeSet files. They make text only.
- A template cannot call itself.
- Expressions cannot read the clock or make random values.
- A
$Nameanchor finds an instance by its browseName. When the expansion gives the same browseName to more than one instance, start the path at a unique name. - The order of the rows in a table is not stable. Do not use
indexto calculate an address in a block that reads a table: put the address in a column.