Template strings
Template strings are expanded using javascript template strings.
Using template strings
Templates can be used in any XML attribute value.
Template strings create string by expanding the the expressions within it.
Expressions are place withing ${}
Expressions can be value names and can include calculations.
"This ${ship} has ${health * 4} health"
If the value ship=”Artemis” and health = 5
"This Artemis has 20 health"
Internally the system uses Javascript Template literals. But most values in the system as strings so adding two value together is likely concatenating string.
You can specify that a value should be treated as an integer, or float using included helper functions. There are functions to convert to the strings to number _.int() _.float()
For example:
"This ${ship} has ${_.int(health) * 4} health"
there is also _.map() to convert
<value x="${_.map.x(2.3)}" y="0.0" z="${_.map.z('B.25')}" navpoint="NAV_01" />
This makes it somewhat easier to had code coordinates on the map.
Extending by importing javascript
Additional javascript functions can be added by adding an import to a javascript file.
<imports>
<import name="myplugin.js" />
</imports>
The javascript needs to expose
exports.MyPlugin = {
message: "Hello, Javascript"
}
The data can then be access in templates scripts.
<expand>
<big-message title="inline ${plugins.MyPlugin.message}"/>
</expand>
Imports with imported javascript
Say you have two xml files, as follows:
template.xml
<imports>
<import name="MyPlugin.js"/>
</imports>
<templates>
<template _name="basic_template">
<params>
</params>
<big-message text="${plugins.MyPlugin.message}"/>
</template>
</templates>
useTemplate.xml
<imports>
<import name="template.xml"/>
</imports>
<expand _template="basic_template"/>
The above will not work properly. Since useTemplate.xml utilizes at template from template.xml, and the template calls a javascript function, useTemplate.xml must also import MyPlugin.js.
For a more complete example see: