String concatenation in OPT
From Invenzzia wiki
New OPT users that got used to PHP language, sometimes have problems with string concatenation in OPT which is a bit different. In this article we are going to show most of the issues that concern this topic.
General string information
In OPT, the strings are written using the single quotes and only using them. The double quotes are reserved for the XML syntax and cannot be used to enclose OPT strings. The concatenation operator is tilde (~), like in D programming language. We were forced to change the concatenation operator, because the dot is already used as a container access operator in template variables and without the information about the variable types and values it is impossible to "guess", what the programmer would mean. So, a string concatenated with a variable would look like this:
'A string '~$variable
Strings and HTML attributes
The HTML attributes are evaluated as strings by default, so you can write them just like in plain HTML files. However, if you wish to make their value dynamic, you have to switch their namespace to parse. Now they will be evaluated as OPT expressions:
<div parse:class="'cssclass '~$customCSS">
...
</div>
Note that now you have to enclose the string within single quotes (unless it is a single word).
Do not enclose the variable names in curly brackets:
{$variable}within OPT expressions! This will cause an exception.
Strings and OPT instruction attributes
The even more interesting situation is with OPT instruction attributes. Some of them may accept only static values (it is mentioned in the documentation), as they are required to compile the instruction properly, but otherwise can be simple expressions. The instruction may expect by default:
- An expression
- A string
However, you can easily switch between those two. For example, consider the opt:attribute instruction which takes two attributes: name and value. They are expected to be expressions and it is convenient, if we are going to read their values from template variables:
<opt:attribute name="$attrName" value="$attrValue" />
But if we do not want a dynamic name? Well, there are two possible solutions:
<opt:attribute name="'staticAttributeName'" value="$attrValue" />
<opt:attribute str:name="staticAttributeName" value="$attrValue" />
They do exactly the same thing and you can use what you find more convenient. However, if you are going to concatenate the string with some value from a variable, you have to use the first way: name="'attributeName'~$variable'"
The opposite way is also possible. opt:include expects a string by default as the name of the template to be included. We can still load it from a variable, if we use the parse namespace:
<opt:include file="some_file.tpl" />
<opt:include parse:file="$variable" />

