VARIABLE
This element ca be used to define custom values to be reused later in the attributes of other elements, in order to avoid repeating several times the same value. It makes it easier to change the value once rather than everywhere in the code.
Note: to avoid conflicts with Predefined Variables, variables starting and ending with '_' are reserved to the KUIML engine.
Attributes brief detailed show all inherited
Name | Description | Default | |
---|---|---|---|
id | id of the variable | empty | |
value | value of the variable | empty | |
formula | The formula can reference other static variables that have been defined previously. The result of the formula is stored in the variable at the declaration of the variable and is not updated later, even if other variables it depends on are changed | empty | |
script | Script that returns a string that defines the new value of the variable. It can use functions that have been defined in the build_time_script_include file (but not other runtime function defined in runtime scripts). It can also refer to build time variables that have been defined before | empty | |
override | override previously defined value | false |
Syntax
First declare a variable as shown here:
- <VARIABLE id="my_variable" value="hello world!" />
Once this variable has been declared, writing the following
- <TEXT value="$my_variable$"/>
is equivalent to writing:
- <TEXT value="hello world!"/>
The same example, with script syntax (note that all special characters in script have to be escaped):
- <VARIABLE id="my_variable" script="return "hello world!"" />
Advanced usage
The possibility to "override" variables is useful in complicated layouts and templates. If you plan to use overriden variables often, then you can add defines like this to shorten the code (already defined in LM Skin):
- <DEFINE>
- <VAR base_type="VARIABLE" override="true" />
- <LV base_type="LOCAL_VARIABLE" override="true" />
- </DEFINE>
Now you can write something like this:
The output should look like this:
You could use "LV" (LOCAL_VARIABLEs) instead, if you don't need these values later (for example a temporary variable inside a TEMPLATE).
Build-time scripts
If you need to do some complicated operations while build-time (usually related to strings) you can use scripts inside VARIABLES, like this:
Check if VARIABLE contains a substring:
- <VAR id="IS_BCA_PLUGIN" script="if ("$PLUGIN_NAME$".findFirst("Blue Cat")>-1) return "1"; else return "0";" />
- <!-- display the result -->
- <TEXT value="IS_BCA_PLUGIN: $IS_BCA_PLUGIN$" />
- <REPEAT count="$IS_BCA_PLUGIN$">
- <TEXT value="This is a Blue Cat Audio plugin: $PLUGIN_NAME$" />
- </REPEAT>
However, if you use too many scripts, you may slow down skin loading time a bit. Also keep in mind, that scripts inside VARIABLEs are executed twice while build-time (for technical reasons), and that can be important if you're manipulating with something like file contents.