pebble icon indicating copy to clipboard operation
pebble copied to clipboard

`empty` test in strict mode fails for undefined map value

Open roytruelove opened this issue 6 years ago • 6 comments

Hello! Could be a bug, could be noob issue.

(version 3.1.0, strict mode on)

I've got a map in Context called p and I try to pull something out of p that doesn't exist (alt in this case):

<span>{{ p.alt }}</span>

The engine (correctly) complains, because I'm in strict mode. So I changed the code to the following expecting it to work:

{% if p.alt is not empty %}<span>{{ p.alt }}</span>{% endif %}

However, the if cannot evaluate because it's choking on trying to eval p.alt (see stacktrace below).

Should this work? If not is there an alternate way?

Thank you for your time and thank you for making pebble!

Caused by: com.mitchellbosecke.pebble.error.PebbleException: Wrong operand(s) type in conditional expression ({% if p.alt is not empty %}<span>{{ p.alt }}</span>{% endif %}:1)
	at com.mitchellbosecke.pebble.node.IfNode.render(IfNode.java:77)
	at com.mitchellbosecke.pebble.node.BodyNode.render(BodyNode.java:43)
	at com.mitchellbosecke.pebble.node.RootNode.render(RootNode.java:30)
	at com.mitchellbosecke.pebble.template.PebbleTemplateImpl.evaluate(PebbleTemplateImpl.java:155)
	at com.mitchellbosecke.pebble.template.PebbleTemplateImpl.evaluate(PebbleTemplateImpl.java:95)
	at com.dotdash.practive.pebble.PebbleMarkupRendererProvider$1.render(PebbleMarkupRendererProvider.java:62)
	at com.dotdash.practive.core.walkers.RenderPhaseComponentWalker.lambda$buildRenderStep$1(RenderPhaseComponentWalker.java:86)
	... 13 common frames omitted
Caused by: com.mitchellbosecke.pebble.error.AttributeNotFoundException: Attribute [alt] of [java.util.Collections$UnmodifiableMap] does not exist or can not be accessed and strict variables is set to true. ({% if p.alt is not empty %}<span>{{ p.alt }}</span>{% endif %}:1)
	at com.mitchellbosecke.pebble.attributes.MapResolver.resolve(MapResolver.java:44)
	at com.mitchellbosecke.pebble.attributes.DefaultAttributeResolver.resolve(DefaultAttributeResolver.java:37)
	at com.mitchellbosecke.pebble.node.expression.GetAttributeExpression.evaluate(GetAttributeExpression.java:82)
	at com.mitchellbosecke.pebble.node.expression.PositiveTestExpression.evaluate(PositiveTestExpression.java:60)
	at com.mitchellbosecke.pebble.node.expression.NegativeTestExpression.evaluate(NegativeTestExpression.java:18)
	at com.mitchellbosecke.pebble.node.IfNode.render(IfNode.java:52)
	... 19 common frames omitted

roytruelove avatar Oct 25 '19 15:10 roytruelove

An improvement was made here with and/or operators https://github.com/PebbleTemplates/pebble/issues/398.

Can you try something like

{% if p is not null and p.alt is not empty %}<span>{{ p.alt }}</span>{% endif %}

ebussieres avatar Oct 25 '19 15:10 ebussieres

Thanks @ebussieres, no luck with that, unfortunately; throws the same error as above. Is this something that's intended to work?

roytruelove avatar Oct 25 '19 20:10 roytruelove

Can you submit a test case for it ?

ebussieres avatar Oct 25 '19 20:10 ebussieres

Can do.

roytruelove avatar Oct 25 '19 20:10 roytruelove

As a workaround I tried to flatten my p map directly into the context, and then tried:

{% if alt is not empty %}<span>{{ alt }}</span>{% endif %}

and even this failed (same error). I also tried null instead of empty and tried without the not. This seems like base-level functionality, no? Maybe no one uses strict mode?

Before I create a test case can you let me know the answer to

Is this something that's intended to work?

If the answer is no then I'm happy to help work on a fix, but if the answer is yes then I've gotta move on to another templating engine if there are no real alternatives to what I'm trying to do

roytruelove avatar Oct 25 '19 21:10 roytruelove

I don't think that strict mode is really used and you're not the first to have this kind of request for this particular use case.

I think that a potential fix would be to not check strict variable if within a "if" node. I don't think that it would be feasible however.

ebussieres avatar Oct 26 '19 17:10 ebussieres