umap
umap copied to clipboard
Conditions on template
It would be interesting, in the popup template, to be able to put as a condition that the field is not null. Something like this :
if ({name} !== null) {name : # {name} }
if ({myfield} !== null) {Foo : {myfield} }
Unless this is already possible? (Please let me know if this is the case! 😉 )
+1 for having the ability to be able to do conditionals. Would be quite useful when dealing with incomplete datasets (common in openstreetmap).
+1
It's a non-trivial feature, especially because it involves security. Some options to consider:
- using John's Resig simple templating (there is a dedicated npm package here) and then DOMPurify the result;
- using Nunjuks from Mozilla but there is also an issue related to untrusted user input;
- creating our own parsing mechanism which also requires sanitization to prevent XSS attacks;
- going full JS syntax (example here) but evaluating JS from users is quite scary 😱.
There is also a potential performance issue to consider if the template is complex and the displayed data is huge.
I wanted to work on this feature (but I don't know the umap codebase). I think the "full JS syntax" solutions looks good and can be made somewhat secure (by shadowing all global variables, so practically giving access only to the variables of interest).
~~One general question is how to remain backward compatible? Should the new syntax be used only if the popup template starts with some key word (like js:)...~~ actually this will become a new popup class
I'm reporting on some explorations. I implemented a prototype for the "full JS (expression) syntax" and I think it is relatively secure, however:
-
many tags contain
:so to havecontact: ${contact:email}work (while it is not a valid js expression), it needs some string replacement of any "letter : letter" for it to work seamlessly. This seems ok for most use cases, it just conflicts with any templates that would contain some "letter : letter", like one that explicitly write tag namescontact:email ${contact:email}(this can also be worked around by some reverse string replacement) -
the question of whether js is adequate for easy conditionals etc remains...
- the best I could get for default values (using _ as a container of all tags, optional chaining and null coalescing)
contact: ${_?.contact:email ?? 'missing'} - and for conditional display is
${'contact:email' in _ ? 'contact: ' + contact:email : ''}
- the best I could get for default values (using _ as a container of all tags, optional chaining and null coalescing)
Hi,
I think that it would be an interesting feature.
For instance, I've schools map and I want to indicate if it exists particularities that doesn't exist in all the schools (an optional teaching or special class). The code I use to display the information in a single line is:
Particularities: {particularitie1}, {particularitie2}, {particularitie3}
Now, if the fields "particularitie1", "particularitie2", "particularitie3" are empty, I've the title "Particularities:" but nothing after. And the displaying is:
Particularities: , ,
So hiding this title would improve the displaying.
The problem is the same with a single field but there's a workaround if the field value contains "none" instead of no value : the line is displayed even there's nothing interesting to show.
Regards,
Thierry
I wonder if we can start with the simple null case and don't allow actual input: {if name} {name} {endif}
We already have minimal null-detection logic with "or" pipes as in {name|description|"none"} so simply detecting emptiness shouldn't be much more work or security issue