variables.rst 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. .. _ysld_reference_variables:
  2. Define and reuse YAML Variables
  3. ===============================
  4. 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.
  5. The two most-common use cases for using variables are:
  6. * To create a more-friendly name for a value (such as using ``myorange`` instead of ``#EE8000``)
  7. * To define a block of directives to remove redundant content and to decrease file length
  8. It is customary, but not required, to place all definitions at the very top of the YSLD file, above all :ref:`header information <ysld_reference_structure>`, :ref:`feature styles <ysld_reference_featurestyles>`, or :ref:`rules <ysld_reference_rules>`.
  9. Syntax
  10. ------
  11. Define a single value
  12. ^^^^^^^^^^^^^^^^^^^^^
  13. The syntax for defining a variable as a single value is::
  14. define: &variable <value>
  15. where:
  16. .. list-table::
  17. :class: non-responsive
  18. :header-rows: 1
  19. :stub-columns: 1
  20. :widths: 20 10 50 20
  21. * - Attribute
  22. - Required?
  23. - Description
  24. - Default value
  25. * - ``define``
  26. - Yes
  27. - Starts the definition block.
  28. - N/A
  29. * - ``&variable``
  30. - Yes
  31. - The name of the variable being defined. The ``&`` is not part of the variable name.
  32. - N/A
  33. * - ``<value>``
  34. - Yes
  35. - A single value, such as ``512`` or ``'#DD0000'``
  36. - N/A
  37. The syntax for using this variable is to prepend the variable name with a ``*``::
  38. <directive>: *variable
  39. This variable can be used in any place where its type is expected.
  40. Define a directive block
  41. ^^^^^^^^^^^^^^^^^^^^^^^^
  42. The syntax for defining a variable as a content block is::
  43. define: &varblock
  44. <directive>: <value>
  45. <directive>: <value>
  46. ...
  47. <block>:
  48. - <directive>: <value>
  49. <directive>: <value>
  50. ...
  51. **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.
  52. .. note:: It is also possible to have nested definitions.
  53. The syntax for using this variable is to prepend the variable name with ``<<: *``. For example::
  54. <block>:
  55. - <directive>: <value>
  56. <<: *varblock
  57. The line that contains the variable will be replaced with the contents of the definition.
  58. Examples
  59. --------
  60. Define variables to reuse colors
  61. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  62. Name background color for both polygon fill and halo outline:
  63. .. code-block:: yaml
  64. define: &background_color '#998088'
  65. define: &text_color "#111122"
  66. feature-styles:
  67. - rules:
  68. - symbolizers:
  69. - polygon:
  70. stroke-width: 1
  71. fill-color: *background_color
  72. - text:
  73. label: ${name}
  74. fill-color: *text_color
  75. halo:
  76. radius: 2
  77. fill-color: *background_color
  78. fill-opacity: 0.8
  79. Define block to reuse stroke
  80. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  81. Define a block of stroke parameters for reuse in each rule:
  82. .. code-block:: yaml
  83. define: &stroke_style
  84. stroke: '#FF0000'
  85. stroke-width: 2
  86. stroke-opacity: 0.5
  87. feature-styles:
  88. - rules:
  89. - filter: ${pop < '200000'}
  90. symbolizers:
  91. - polygon:
  92. <<: *stroke_style
  93. fill-color: '#66FF66'
  94. - filter: ${pop BETWEEN '200000' AND '500000'}
  95. symbolizers:
  96. - polygon:
  97. <<: *stroke_style
  98. fill-color: '#33CC33'
  99. - filter: ${pop > '500000'}
  100. symbolizers:
  101. - polygon:
  102. <<: *stroke_style
  103. fill-color: '009900'