Variables in YSLD that allow for a certain directive or block of directives to be defined by name and later reused. This can greatly simplify the styling document.
The two most-common use cases for using variables are:
- To create a more-friendly name for a value (such as using myorange instead of #EE8000)
- To define a block of directives to remove redundant content and to decrease file length
The syntax for defining a variable as a single value is:
define: &variable <value>
|define||Yes||Starts the definition block.||N/A|
|&variable||Yes||The name of the variable being defined. The & is not part of the variable name.||N/A|
|<value>||Yes||A single value, such as 512 or '#DD0000'||N/A|
The syntax for using this variable is to prepend the variable name with a *:
This variable can be used in any place where its type is expected.
The syntax for defining a variable as a content block is:
define: &varblock <directive>: <value> <directive>: <value> ... <block>: - <directive>: <value> <directive>: <value> ...
Any number of directives or blocks of directives can be inside the definition block. Moreover, any type of directive that is valid YSLD can be included in the definition, so long as the content block could be substituted for the variable without modification.
It is also possible to have nested definitions.
The syntax for using this variable is to prepend the variable name with <<: *. For example:
<block>: - <directive>: <value> <<: *varblock
The line that contains the variable will be replaced with the contents of the definition.
The following are all examples of variable substitution
Name a color:
define: &myorange '#EE8000'
Reusable text string:
define: &rulename "This is my rule"
define: &strokestyle stroke: '#FF0000' stroke-width: 2 stroke-opacity: 0.5
polygon: <<: *strokestyle