diff --git a/README.md b/README.md index 4e89d2dc..496590af 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ There are several extra functions defined for the TwigJS language: * 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'). * 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'). * function `trans`: return the translation of the given string (e.g. 'save', 'unknown', 'unnamed', ...). Parameters: string (the string to translate). +* 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:"). Notes: * Variables will automatically be HTML escaped, if not the filter raw is used, e.g.: {{ tags.name|raw }} diff --git a/src/index.js b/src/index.js index 696e4fcd..190d6861 100644 --- a/src/index.js +++ b/src/index.js @@ -21,6 +21,7 @@ require('./language') require('./location') require('./overpassChooser') require('./fullscreen') +require('./twigFunctions') window.onload = function() { map = L.map('map') diff --git a/src/twigFunctions.js b/src/twigFunctions.js new file mode 100644 index 00000000..7717c23b --- /dev/null +++ b/src/twigFunctions.js @@ -0,0 +1,19 @@ +var OverpassLayer = require('overpass-layer') + +OverpassLayer.twig.extendFunction('tagsPrefix', function (tags, prefix) { + var ret = {} + var count = 0 + + for (var k in tags) { + if (k.substr(0, prefix.length) === prefix) { + ret[k.substr(prefix.length)] = k + count++ + } + } + + if (count == 0) { + return null + } + + return ret +})