I have jinja2 template which get data variables from yaml in the form of 'keys : value'.
The issue is when I run jinja2 template, it will print the line that has empty 'value'.
How can I do so that it will not print the line that is empty. Otherwise, it will appear None.
For example;
-Template
set first {{ABC}}
set second {{DEF}}
set third {{GHI}}
-yaml data
ABC : data1
DEF :
GHI : data3
If I run this, it will state that;
set first data1
set second None
set third data3
I don't want it to be this way. I want it to print;
set first data1
set third data3
How to do that?by preventing line from being added if the variable is undefined.
Thanks,
Updated script: -
TEMPLATE_FILE = { 'trial_1': 'trial.tcl' }
for f,s in TEMPLATE_FILE.items():
template = templateEnv.get_template( s )
with open('tmp.yaml', 'rb') as stream:
try:
yaml_input_data = yaml.load(stream)
except yaml.YAMLError as exc:
pass
print(yaml_input_data)
for k, v in yaml_input_data.items():
if v is None:
yaml_input_data.pop(k)
print(yaml_input_data)
with open(f, 'w') as outfile:
outfile.writelines(template.render(yaml_input_data))
print '\nWrote',f
Updated jinja2 template
{% for k, v in yaml_input_data.iteritems() %}
{# use k, v #}
set first {{ABC}}
set second {{DEF}}
set third {{GHI}}
{% endfor %}