Skip to main content

Templates: reference

This page gives the rules of templates. Templates gives the procedures.

Key reference​

KeyInTypeMeaning
paramstemplateobjectThe params of the template, by name. A template sees only its params and parent.
countertemplateobject{ 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.
repeattemplateobjectRepeat the body of the template. { count, index }.
instancestemplatearrayThe instances that each use adds. An - expand: entry here is a nested block.
mappingtemplatearrayThe mapping entries that each use adds, at the end of mapping:.
initializerstemplatearrayThe initializers that each use adds, at the end of initializers:.
expandtemplateExpandBlockA block that runs after the entries of the template. Use it to make children.
parentexpand blockstringThe parent of the entries. Templates read it as ${parent}. Without it, the block uses the parent of the enclosing block.
withexpand blockobjectValues that are the same for all iterations. The block gives them to each template that declares a param with that name.
countersexpand blockobjectThe counters of the block, by name. A counter lives in this block and in the blocks in it.
useexpand blockstringThe template that each iteration uses.
layoutexpand blockarrayThe templates that each iteration uses, in order. An entry is a name, { use, repeat, with } or { expand }.
repeatexpand blocknumber or stringThe number of iterations. An expression can give it.
indexexpand blockTemplateIndexThe value of each iteration. n gives 0, 1, 2. A map gives a counter with a name: { name: n, start: 1, format: "000" }.
overexpand blockarray or objectOne iteration for each record. A list of maps, or a CSV table { file }.
reverseexpand blockboolean or stringThe counters of the block give their values from the other end. The order of the entries does not change.
instancesexpand blockarrayInstances that each iteration adds, without a template.
mappingexpand blockarrayMapping entries that each iteration adds, without a template.
initializersexpand blockarrayInitializers that each iteration adds, without a template.
startcounternumberThe first value. The default is 0.
stepcounternumberThe increase between two values. The default is 1.
formatcounterstringA run of "0". The value becomes text, with zeros on the left to this width: "00" gives 01, 02.
incounterarrayThe values, in order. Do not use it with start or step.
typeparam"string", "number", "boolean"The type of the value. A value of another type is an error.
defaultparamanyThe value when the block gives no value. Without a default, the param is required.
enumparamarrayThe only values that the param accepts.
inheritparambooleanWhen the block gives no value, take the value of the same name from the enclosing template call.
filetable (over:)stringThe CSV table, relative to the configuration file. Its first line names the columns.
delimitertable (over:)",", ";"The separator of the cells. Without it, the expander finds it from the first line.
decimalSeparatortable (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 the with: of the layout entry. The with: 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: true gets the value of the same name from the enclosing template call when the block gives no value.
  • A param without inherit: true never 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.
  • $name without braces is not a template expression. In a mapping: entry, $raw stays 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 a layout: and in a module, the params of the used template in with: and in an over: record, and the counter names in counter:.
  • 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 $Name anchor 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 index to calculate an address in a block that reads a table: put the address in a column.