umap icon indicating copy to clipboard operation
umap copied to clipboard

Conditions on template

Open SMoONF61 opened this issue 4 years ago • 7 comments
trafficstars

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! 😉 )

SMoONF61 avatar Feb 19 '21 16:02 SMoONF61

+1 for having the ability to be able to do conditionals. Would be quite useful when dealing with incomplete datasets (common in openstreetmap).

donalhunt avatar May 19 '22 20:05 donalhunt

+1

chnuessli avatar Aug 20 '22 14:08 chnuessli

It's a non-trivial feature, especially because it involves security. Some options to consider:

There is also a potential performance issue to consider if the template is complex and the displayed data is huge.

davidbgk avatar May 19 '23 14:05 davidbgk

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

twitwi avatar Feb 10 '24 17:02 twitwi

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 have contact: ${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 names contact: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 : ''}

twitwi avatar Feb 13 '24 10:02 twitwi

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

ThierryM avatar Mar 26 '24 08:03 ThierryM

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

zyphlar avatar May 17 '24 07:05 zyphlar