Discussions

 View Only
  • 1.  Jinja Dynamic Variable Name using Loop Index

    Posted 09-24-2022 20:32
    Assuming I've two fields in my table

    son1
    son2


    I'm trying to create two records in another table using jinja loop(which is happening) but the dynamic names are not out of my reach


    "to": "bsp8bd9qp",
    "data": [
    {% for i in range(2) %}
    { "17": { "value" : "{{a.son~loop.index}}" }}
    {% if loop.last == false %},{% endif %}
    {% endfor %}
    ]
    }

    Below works but predictably gives me value for only son1 and not son2
    { "17": { "value" : "{{a.son1~loop.index}}" }}


    ------------------------------
    Prashant Maheshwari
    ------------------------------


  • 2.  RE: Jinja Dynamic Variable Name using Loop Index

    Posted 09-25-2022 09:33
    To reference the son1 and son2 fields using a variable, you need to refer to each as a['son1'] and a['son2'].  Try this:

    "to": "bsp8bd9qp",
    "data": [
    {% for i in range(2) %}
    { "17": { "value" : "{{a['son'~loop.index]}}" } }
    {% if loop.last == false %},{% endif %}
    {% endfor %}
    ]
    }​

    Hope that helps!

    ------------------------------
    Doug Henning
    ------------------------------



  • 3.  RE: Jinja Dynamic Variable Name using Loop Index

    Posted 09-25-2022 11:43
    Edited by Prashant Maheshwari 09-25-2022 11:50

    DOUG !!!

    MY BIG FAT THANKS TO YOU !!!

    You've helped me cut down my jinja query from 30 lines to few handful .

    THANK YOU SO MUCH !!!

     adding you to friend list for PM , please accept

    ------------------------------
    Prashant Maheshwari
    ------------------------------