Declarations in template files
Declarations at the start of a template file define names that you can use in the rest of the file: variables,
dictionaries and user-defined functions. Every declaration starts with the keyword LET and ends with a semicolon.
You write LET, INDEX and BY in upper case, and type names in lower case. You do not declare callable templates
here, but in template sections (see
Template section). Variables, dictionaries,
user-defined functions and callable templates share one set of names, so you can declare a name only once in a
template file.
Global variable
A global variable is a named value that EFX computes once, and that you can use everywhere in the template file.
LET <type>:$<variable-name> = <expression>;
- <type>
-
the data type of the variable:
text,number,indicator,date,timeorduration, or one of the sequence typestext*,number*,indicator*,date*,time*,duration*. - <variable-name>
-
the name of the variable (see Identifiers).
- <expression>
-
the expression that gives the value of the variable. Its type must be the declared type.
You declare a global variable with the syntax of a
variable declaration, among the
global declarations at the start of the template file. You refer to it as $<variable-name>.
EFX evaluates the expression with the root node of the notice as its context: it looks for the fields and nodes of the expression in the whole notice. So, to hold a field that can have several values in a notice, the variable must have a sequence type.
The expression can use the global variables, dictionaries and user-defined functions declared before the variable. You can use the variable in the declarations that follow it, in the callable templates, in all template lines and in the sections.
Restrictions
-
You must declare a global variable before you use it.
-
You cannot use the name of a global variable again for another declaration, for a parameter, or for a variable of a template line.
-
You cannot declare context variables (
context:) or dynamic variables (dynamic:) as global variables of a template file.
Examples
LET text:$separator = ' / ';
LET number*:$thresholds = [10, 100, 1000];
LET text*:$all-texts = BT-00-Repeatable-Text;
LET number:$text-count = count($all-texts);
WITH ND-Root DISPLAY ${$text-count} texts: ${string-join($all-texts, $separator)};
WITH BT-00-Number WHEN BT-00-Number > $thresholds[at 2] DISPLAY #{name}: $value;
Global variables of single and sequence types, one of them computed from another.
view-templates/1.efx)LET text*:$tender_ids_global = OPT-321-Tender;
| Global variables are new in EFX-2. |
Dictionary declaration
A dictionary declaration indexes the values of a field by a key. You can then look up the values by their key from anywhere in the template file. With a dictionary, you do not need to repeat a predicate in every place where you need the same kind of lookup, and the application that displays the notice can build the index only once.
LET $<dictionary-name> INDEX <field> BY <key>;
- <dictionary-name>
-
the name of the dictionary.
- <field>
-
the field whose values the dictionary indexes: a field reference, which can be absolute and can have predicates and an index.
- <key>
-
an expression that returns one text value: the key of each indexed value.
The dictionary contains every occurrence of the field in the notice. For each occurrence, EFX evaluates the key expression with that occurrence of the field as its context. The dictionary then links each key to the values of the occurrences of the field that have this key. Several occurrences can have the same key.
You read the values in a dictionary with a dictionary lookup.
Restrictions
-
The dictionary must index a field. A node is not accepted.
-
The key must be a single text value for each occurrence of the field. Convert a number or a date first, for example with
text(…). -
You can declare a dictionary only in a template file.
Examples
LET $text-by-attribute INDEX BT-00-Text-In-Repeatable-Node BY BT-00-Attribute-In-Repeatable-Node;
LET $text-by-number INDEX BT-00-Text-In-Repeatable-Node BY text(BT-00-Number-In-Repeatable-Node);
LET $date-by-key INDEX BT-00-Date-In-Repeatable-Node[BT-00-Text-In-Repeatable-Node != ''] BY concat(BT-00-Text-In-Repeatable-Node, '-', BT-00-Attribute-In-Repeatable-Node);
The first dictionary indexes texts by an attribute of the same XML element, and the second by a number converted to text. The third indexes some dates by a key made of two fields.
view-templates/1.efx)LET $lot-name-by-lot-or-gol-id INDEX BT-21-Lot BY BT-137-Lot;
| Dictionaries are new in EFX-2. |
See also: Predicate
Dictionary lookup
A dictionary lookup returns the values that a dictionary holds for a key.
$<dictionary-name>[<key>]
- <dictionary-name>
-
the name of a dictionary declared in the template file (see Dictionary declaration).
- <key>
-
an expression that returns one text value.
A dictionary lookup is an expression. You can use it in expression blocks, in conditions, in the value of a variable, in the body of a user-defined function and in the arguments of a template invocation. EFX evaluates the key in the context where you write the lookup.
The lookup returns a sequence: the values of all the indexed occurrences of the field whose key is equal to the given key. The sequence is empty when no occurrence has this key. The values have the type of the indexed field.
The result is a sequence. So, where a single value is expected, you must select one of the values, for example the
first one with [at 1] (see Index).
Restrictions
-
A lookup where a single value is expected, without an index that selects one value, is an error.
-
The name of a dictionary is not an expression on its own:
$<dictionary-name>without a key is an error. -
The key must be a single text: from
ND-Root,$text-by-attribute[BT-13-Text]is an error.
Examples
LET $text-by-attribute INDEX BT-00-Text-In-Repeatable-Node BY BT-00-Attribute-In-Repeatable-Node;
WITH ND-Root DISPLAY ${$text-by-attribute['a1']};
WITH ND-Root DISPLAY First text: ${$text-by-attribute['a1'][at 1]};
WITH ND-Root WHEN 'x' in $text-by-attribute['a1'] DISPLAY Found;
WITH BT-00-Text DISPLAY ${count($text-by-attribute[BT-00-Text])};
WITH BT-00-Text DISPLAY ${$text-by-attribute[upper-case(BT-00-Text)][at 1]};
All the values for a key, the first value for a key, a test on the values for a key, a lookup whose key is the value of the context field, and a lookup with a key computed from it.
| Dictionary lookups are new in EFX-2. |
See also: Membership (in, not in)
User-defined function declaration
A user-defined function declaration defines a function that has a name and a type, and computes a value from its parameters. You can then call the function from any expression in the template file.
LET <return-type>:?<function-name>(<parameter>, <parameter>, …) = <expression>; LET <return-type>:?<function-name>() = <expression>;
- <return-type>
-
the type of the value returned:
text,number,indicator,date,timeorduration, or one of the sequence typestext*,number*,indicator*,date*,time*,duration*. - <function-name>
-
the name of the function (see Identifiers).
- <parameter>
-
a parameter, written
<type>:$<parameter-name>, where<type>is one of the types listed for<return-type>. Optional: a function can have no parameters. - <expression>
-
the body of the function. Its type must be the return type.
You call a user-defined function with a question mark before its name: ?<function-name>(<argument>, …) (see
User-defined function call).
Each call evaluates the body with the parameters set to the values of the arguments. The translator checks the number
and the types of the arguments against the parameters.
EFX evaluates the body with the root node of the notice as its context, wherever you call the function. So you must pass values that depend on the context of the call as arguments. The body can use the parameters, and the global variables, dictionaries and user-defined functions declared before the function.
User-defined functions exist in template files only. Rules files call external services through dynamic functions instead (see Dynamic function declaration).
Restrictions
-
You must declare a function before you call it. A function cannot call itself.
-
The type of the body must be the return type. For a function of a sequence type, such as
text*, a body that gives a single value gives a sequence of one value. -
Parameters cannot be of the type
context. -
Parameter names must be different from the names of the global variables declared before the function. Two functions can have parameters with the same name.
-
You cannot declare a user-defined function in a rules file.
Examples
LET text:?label-and-value(text:$label, text:$value) = concat($label, ': ', $value);
LET indicator:?is-large(number:$amount) = $amount > 1000;
LET number*:?doubled(number*:$values) = for number:$v in $values return $v * 2;
WITH BT-00-Number WHEN ?is-large(BT-00-Number) DISPLAY ${?label-and-value('Large', text(BT-00-Number))};
WITH ND-Root DISPLAY ${sum(?doubled(BT-00-Repeatable-Number))};
Three functions, called in a condition and in expression blocks.
LET text:?greeting() = 'Hello';
LET text:?greet(text:$name) = concat(?greeting(), ' ', $name);
DISPLAY ${?greet('world')};
A function without parameters, called by another function.
view-templates/1.efx)LET indicator:?exists-common-value(text*:$seq1, text*:$seq2) = (count(value-intersect($seq1, $seq2)) > 0);
| User-defined functions are new in EFX-2. |
See also: Sequence