0

With Jinja2, is there any way to identify the variable used in PARSER before doing the for loop?

 Context = """
   {% macro LopyLoop(PARSER) %}
      {% for items in PARSER -%}
         {{ items }}{% if not loop.last %},{% endif %}
      {%- endfor %}
   {% endmacro %}
 
   {% set Mainlist =  ['ABC','XYZ']  %}
 
   {{ LopyLoop(Mainlist) }}
   {{ LopyLoop(Mainlist[0]) }}
 
 )
  """
 Output = Template(Context)
 print Output.render()

The above is valid is valid when using ["ABC","XYZ"] but is not when using a string Mainlist[0]

I can't find the a way to solve this one.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
vcapp
  • 1,877
  • 2
  • 15
  • 23

1 Answers1

0

Looking around i found this link Looping over subset in Jinja

The answer is to {{ LopyLoop(Mainlist[0:1]) }}

Community
  • 1
  • 1
vcapp
  • 1,877
  • 2
  • 15
  • 23