Crinja - how to get value from bindings via the variable


Suppose I send the following variables (bindings) to the template:

  "var1" => 1
  "var2" => "aaa"

And in the template I can access variables/bindings like this:

{{ var1 }}

but what if I want to get some value by the content of the variable, is there some accessor for globals, or all_variables?

{% my_key = "var2" %}
{{ something_like_globals[my_key] }} # error - no globals/undefined

I know how to do it for some sub-dictionary, like this:

  "var1" => 1
  "var2" => "aaa"
  "var3" => { "sub" => "bbb" }

Then in the template I can do this:

{% my_key = "sub" %}

{{ var3[my_key} }}

But I don’t know how to reach those root bindings.
I tried looking in the documentation - but no luck.


I don’t think this is possible in Crinja. Would you happen to know if there’s a way to do this in Jinja2?

A simple solution would be to assign the variables to a nested variable to be able to use a named accessor.
Or you could define a custom function for this.

Crinja.function({name: Crinja::UNDEFINED}, :var) do

Crinja.render(%({{ var("foo") }}), { "foo" => "bar" }) # => "bar"
1 Like

This trick with custom Crinja function works like a charm. I learned something. It’s a cool shard!