You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
6.7 KiB

  1. #### Examples
  2. Twig resp. TwigJS is a template language. Example:
  3. ```twig
  4. Value of property "test": {{ test }}.
  5. ```
  6. If-condition:
  7. ```twig
  8. {% if test == "foo" %}
  9. It's foo!
  10. {% elseif test == "bar" %}
  11. It's bar!
  12. {% else %}
  13. Other value: {{ test }}
  14. {% endif %}
  15. ```
  16. For-loop:
  17. ```twig
  18. {% for k, v in tags %}
  19. Tag {{ k }} has value {{ v }}
  20. {% endfor %}
  21. ```
  22. Assign value to variable:
  23. ```twig
  24. {% set k = "foo" %}
  25. ```
  26. For more information, please visit:
  27. * [Page of the original Twig template language](https://twig.symfony.com/)
  28. * [Wiki of the TwigJS template language which is almost identical to Twig](https://github.com/twigjs/twig.js/wiki)
  29. #### TwigJS templates
  30. When rendering map features, the following properties are available:
  31. * `id` (the id of the object is always available, prefixed 'n' for nodes, 'w' for ways and 'r' for relations; e.g. 'n1234')
  32. * `osm_id` (the numerical id of the object)
  33. * `layer_id` (the id of the category)
  34. * `type` ('node', 'way' or 'relation')
  35. * `tags.*` (all tags are available with the prefix tags., e.g. tags.amenity)
  36. * `meta.timestamp` (timestamp of last modification)
  37. * `meta.version` (version of the object)
  38. * `meta.changeset` (ID of the changeset, the object was last modified in)
  39. * `meta.user` (Username of the user, who changed the object last)
  40. * `meta.uid` (UID of the user, who changed the object last)
  41. * `map.zoom` (Current zoom level)
  42. * `const.*` (Values from the 'const' option)
  43. * `user.*` (Values from the user's options, e.g. `user.ui_lang`, `user.data_lang`, ...)
  44. For the info-section of a category the following properties are available:
  45. * `layer_id` (the id of the category)
  46. * `map.zoom` (Current zoom level)
  47. * `const.*` (Values from the 'const' option)
  48. There are several extra functions defined for the TwigJS language:
  49. * function `keyTrans`: return the translation of the given key. Parameters: key (required, e.g. 'amenity').
  50. * function `tagTrans`: return the translation of the given tag. Parameters: key (required, e.g. 'amenity'), value (required, e.g. 'bar'), count (optional, default 1).
  51. * function `tagTransList`: return the translations of the given tag for tags with multiple values separated by ';' (e.g. 'cuisine'). Parameters: key (required, e.g. 'cuisine'), value (required, e.g. 'kebab' or 'kebab;pizza;noodles;burger').
  52. * function `localizedTag`: return a localized tag if available (e.g. 'name:de' for the german translation of the tag). Parameters: tags (the tags property), key prefix (e.g. 'name'). Which language will be returned depends on the "data language" which can be set via Options. If no localized tag is available, the tag value itself will be returned (e.g. value of 'name').
  53. * function `trans`: return the translation of the given string (e.g. 'save', 'unknown', 'unnamed', ...). Parameters: string (the string to translate).
  54. * function `repoTrans`: translate strings from this repositories' language file (located in `lang/xy.json`, where `xy` stands for the current locale). The string in the language file can include sprintf placeholders (Use the [sprintf-js module](https://www.npmjs.com/package/sprintf-js). Additional parameters to repoTrans will be passed as arguments to the sprintf function.
  55. * function `tagsPrefix(tags, prefix)`: return all tags with the specified prefix. The result will be an array with `{ "en": "name:en", "de": "name:de" }` (for the input `{ "name": "foo", "name:en": "english foo", "name:de": "german foo" }` and the prefix "name:").
  56. * function openingHoursState(opening_hours_definition): returns state of object as string: 'closed', 'open' or 'unknown'.
  57. * function colorInterpolate(map, value): interpolates between two or more colors. E.g. `colorInterpolate([ 'red', 'yellow', 'green' ], 0.75)`.
  58. * function enumerate(list): enumerate the given list, e.g. "foo, bar, and bla". Input either an array (`enumerate([ "foo", "bar", "bla" ])`) or a string with `;` as separator (`enumerate("foo;bar;bla")`).
  59. * function debug(): print all arguments to the javascript console (via `console.log()`)
  60. Extra filters:
  61. * filter websiteUrl: return a valid http link. Example: `{{ "www.google.com"|websiteUrl }}` -> "http://www.google.com"; `{{ "https://google.com"|websiteUrl }}` -> "https://google.com"
  62. * filter `matches`: regular expression match. e.g. `{{ "test"|matches("e(st)$") }}` returns `[ "est", "st" ]`. You can pass a second parameter with [RegExp flags](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp). Returns null if it does not match.
  63. * filter `osmParseDate`: returns an array with the lower and upper boundary of the year of a `start_date` tag. See [openstreetmap-date-parser](https://github.com/plepe/openstreetmap-date-parser) for details.
  64. * filter `osmFormatDate`: returns the date as localized strings. Accept an object for options, e.g. `{{ tags.start_date|osmFormatDate({ format: 'short' }) }}`. See [openstreetmap-date-format](https://github.com/plepe/openstreetmap-date-format) for details.
  65. * filter `natsort`: Sort an array naturally, see [natsort](https://www.npmjs.com/package/natsort) for details.
  66. * filter `ksort`: Sort an associative array by key (alphabetic)
  67. * filter `unique`: Remove duplicate elements from an array.
  68. * filter `md5`: calculate md5 hash of a string.
  69. * filter `enumerate`: enumerate the given list, e.g. "foo, bar, and bla". Input either an array (`[ "foo", "bar", "bla" ]|enumerate`) or a string with `;` as separator (`"foo;bar;bla"|enumerate`).
  70. * filter `debug`: print the value (and further arguments) to the javascript console (via `console.log()`)
  71. * filter `wikipediaAbstract`: shows the abstract of a Wikipedia article in the selected data language (or, if not available, the language which was used in input, resp. 'en' for Wikidata input). Input is either 'language:article' (e.g. 'en:Douglas Adams') or a wikidata id (e.g. 'Q42').
  72. * filter `wikidataEntity`: returns the wikidata entity in structured form (or `null` if the entity is not cached or `false` if it does not exist). Example: https://www.wikidata.org/wiki/Special:EntityData/Q42.json
  73. * filter `json_pp`: JSON pretty print the object. As parameter to the filter, the following options can be passed:
  74. * `indent`: indentation (default: 2)
  75. * filter `yaml`: YAML pretty print the object. As options the filter, all options to [yaml.dump of js-yaml](https://github.com/nodeca/js-yaml#dump-object---options-) can be used.
  76. Notes:
  77. * Variables will automatically be HTML escaped, unless the filter raw is used, e.g.: `{{ tags.name|raw }}`
  78. * The templates will be rendered when the object becomes visible and when the zoom level changes.
  79. * If you set an arbitrary value within a twig template (e.g.: `{% set foo = "bar" %}`), it will also be available in further templates of the same object by using (e.g.: `{{ foo }}`). The templates will be evaluated in the order as they are defined.