Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

escape dot in variable names in jinja template

Tags:

python

jinja2

I have jinja template file with variable names like x.y.z (like java package names). Then I'm using Python envtpl to generate actual config file from jinja template file using envtpl.process_file. But it errors out saying 'x' is undefined. I know in jinja dot means something else so how do I escape it so that envtpl understands that 'x.y.z' is a variable name not just 'x'.

template file for example:

foo = "{{ x.y.z | default("abc") }}"
bar = "{{ a.b.c | default("123") }}"
like image 681
Himanshu Shah Avatar asked Nov 20 '22 15:11

Himanshu Shah


1 Answers

If someone is still interested in this, this worked for me:

/* model */
...
some_function('some.value')
...



{# macro #}
{% macro some_function(value) %}
    {% set data = {{ "'"+value+"'" }} %}
    {{ data }}
{% endmacro %} 

OR

{# macro #}
{% macro some_function(value) %}
    {% set data = {{ "'%s'" % value }} %}
    {{ data }}
{% endmacro %}
like image 78
Teresita Guerrero Avatar answered Dec 05 '22 13:12

Teresita Guerrero