j2cli
j2cli copied to clipboard
Variable array in the .ini file
Dear, I have a really noob issue, please be forgiving. Is that tool capable of reading a variable list? I guess it is but I can't figure out the syntax as all examples have single value for a variable name I have a .ini that I think should look like that.
[sensors]
name=aaa,bbb,ccc
and a template that
{% for name in sensors %}
Sensor name is {{ sensors.name }}
{% endfor %}
I was hoping that would get me the following result/
Sensor name is aaa
Sensor name is bbb
Sensor name is ccc
Instead I get the file with
Sensor name is aaa,bbb,ccc
The list is not iterated. What am I missing?
I am seeing the same issue with --format=env
It would be nice if there is a way to do {% for x in y%}
A test template:
{% for x in lists %} - {{ x }} {% endfor %}
env file:
lists=( aa bb cc )
run:
j2 --format=env test.html env.txt
- (
-
- a
- a
-
- b
- b
-
- c
- c
-
- )
The easiest way is to use string splitting.
{% set sensorlist = sensors.name.split(',') -%}
{% for name in sensorlist -%}
Sensor name is {{ name }}
{% endfor %}
with your .ini file yields
Sensor name is aaa
Sensor name is bbb
Sensor name is ccc
confirmed @sbliven solution works.
I can concat my items with comma and set the envvar, then split it into a list inside the template
Thanks!
Hi! This project gets some interest, but sadly, I'm not maintaining it =\
Maintainer wanted! Discussion: Maintainer Wanted