tyler-sommer / stick

A golang port of the Twig templating engine
MIT License
183 stars 36 forks source link

Error when accessing missing key in map #43

Closed Richtermeister closed 3 years ago

Richtermeister commented 3 years ago

Hi there, (love this package, thank you soooo much for it!!)

I'm getting an error when I access a missing key in a map (originating here: https://github.com/tyler-sommer/stick/blob/master/value.go#L242) and I don't seem to find a way to suppress/avoid it.

In Twig, there is a strict_variables setting which causes this access to return null (https://twig.symfony.com/doc/3.x/templates.html#variables) and in twig I could check for the existence of the variable without triggering the error.

Specifically, I'm using this package to render arbitrary json data provided as map[string]interface{}, for example:

{ "data": { "foo": "bar" }}

and I'm trying to handle the expression {{ data.missing }} without running into the error. I would like it to just output empty string if missing is not defined on data.

Am I overlooking a way to do this? Thank you for your help!

Richtermeister commented 3 years ago

Ooops, duplicate of https://github.com/tyler-sommer/stick/pull/40 Apologies, I was a few versions behind.