j2cli icon indicating copy to clipboard operation
j2cli copied to clipboard

Variable array in the .ini file

Open xsherlockpl opened this issue 5 years ago • 5 comments

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?

xsherlockpl avatar Jan 08 '20 21:01 xsherlockpl

I am seeing the same issue with --format=env

It would be nice if there is a way to do {% for x in y%}

admun avatar Feb 19 '20 16:02 admun

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

-

- )

admun avatar Feb 19 '20 16:02 admun

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

sbliven avatar Feb 26 '20 12:02 sbliven

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!

admun avatar Mar 04 '20 21:03 admun

Hi! This project gets some interest, but sadly, I'm not maintaining it =\

Maintainer wanted! Discussion: Maintainer Wanted

kolypto avatar Feb 10 '24 16:02 kolypto