Forum Discussion

PrashantMaheshw's avatar
PrashantMaheshw
Qrew Captain
3 years ago

(Video) How to generate children from simple text field using Jinja

I've been using below methodology at LOT for quickly capturing info on the go on mobile , this is also used by my assistant for quick capturing information in 1 place during meetings. 

Why am I posting this ? Barely any information exists on JINJA on forums and online from pipeline perspective. 

PS : This is a Version 1 video , I do skip through explanation of jinja code thoroughly , if people demand , version 2 would be created 

Jinja - Generated Children through text fields
Loom remove preview
Jinja - Generated Children through text fields
View this on Loom >



------------------------------
Prashant Maheshwari
------------------------------
  • Thx for posting!

    ------------------------------
    Mark Shnier (Your Quickbase Coach)
    mark.shnier@gmail.com
    ------------------------------
  • Very interesting! Thanks for posting!

    ------------------------------
    Forrest Parker
    ------------------------------
  • DougHenning1's avatar
    DougHenning1
    Community Manager
    Thank you for making the video and sharing your knowledge with others!  For the sake of learning, I'd like share some improvements you can make to the Jinja for more streamlined code. 

    1. Try not to use variables that are also the names of filters (list is a filter). While it will work most of the time, it can cause confusion.

    2. The namespace functionality is very useful, but in your case you don't actually need it.  I assume you're using it because some lines may be blank and you don't want to count them.  If you filter the list to exclude the blank lines then you can just use the loop index instead:
    {% for x in plist if x|trim != '' -%}​


    3. Use the variable specified in the for loop (x) instead of list[loop.index0]

    4. You can use loop.last to see if you need to add the comma:

    {{ ',' if not loop.last }}

    Here's the complete updated code:
    {% set plist = a.quick_capture.split('\n') -%}
    {
      "to": "br8hpcpk7",
      "data": [
        {% for x in plist if x|trim != '' -%}
          {
            "18": { "value": "{{a.related_director}}" },
            "89": { "value": "{{a.id}}" },
            "6": { "value": "{{loop.index}} {{ x|trim }}" }
          }{{ ',' if not loop.last }}
        {% endfor -%}
      ]
    }


    Thanks again for sharing and hope this helps!



    ------------------------------
    Doug Henning
    ------------------------------
  • First, thank you.  The concept of a "Quick Capture" is going to be helpful with many business cases.

    Second, I am going to have to watch this several times and then come back to you with questions.


    ------------------------------
    Don Larson
    ------------------------------
    • PrashantMaheshw's avatar
      PrashantMaheshw
      Qrew Captain
      Thank you Don, Mark and Forrest for kind encouragement.  All future questions are appreciated. Mark do try running Quick Capture  on a mobile page , it's a thing of beauty 

      Thank you @Doug Henning for detailed reply .Your reply and insights have made it worth to create and share.

      I have one question . In my trial errors , I found if I only write {{x}} as in ("6": { "value": "{{loop.index}} {{x}}" } ,  output was always empty . I still don't understand why?
      "6": { "value": "{{loop.index}} {{ x|trim }}" }
      ​




      {% for x in plist if x|trim != '' -%}​​

      Yes you are right , namespace was used specifically to have counter variable inside the loop for avoiding blank lines. I had no clue , there was a concept of conditional loop , this will help me tonnes of my other much longer jinja code !

      {{ ',' if not loop.last }}

      Again thank you for this much simpler code , I copy pasted {% if not loop.last %},{% endif %} from one of the pipeline sessions .

      THANKS for the much simpler and easy to read code



      ------------------------------
      Prashant Maheshwari
      ------------------------------
      • DougHenning1's avatar
        DougHenning1
        Community Manager
        Hi @Prashant Maheshwari glad to help!  I'm not sure why using {{x}} isn't working for you.  That's standard Jinja, so maybe something else is causing the issue?

        Do you do all your Jinja testing in Pipelines? I like to use an online Jinja parser like this one (https://j2live.ttl255.com)  to test since it's quicker than running Pipelines after each change.  Let's try it out and test the code!

        1. Put the Jinja code in the Template box:
        {%- set plist = a.quick_capture.split('\n') -%}
        {
          "to": "br8hpcpk7",
          "data": [
            {%- for x in plist if x|trim != '' %}
              {
                "18": { "value": "{{a.related_director}}" },
                "89": { "value": "{{a.id}}" },
                "6": { "value": "{{loop.index}} {{ x|trim }}" }
              }{{ ',' if not loop.last }}
            {%- endfor %}
          ]
        }​

        2. Create a sample record structure in the Data input box.  This simulates a Pipeline step A so data can be referenced as {{a.id}}:
        {
            "a": {
                "quick_capture": "testing\none\n\ntwo\nthree",
                "id": 12345,
                "related_director": "bob testing"
            }
        }


        3. ​Click the [Render Template] button and you should get the results in Rendered Template box:


        NOTE: The online parsers don't support all the filters that Pipelines provides (e.g. time) but can help with most Jinja.

        Regarding {% if not loop.last %},{% endif %} vs {{ ',' if not loop.last }}, both are certainly acceptable.  I tend to prefer the second for easy statements and the first for more involved if/then/else cases, but use whatever works best for you!

        Hope that's helpful!

        ------------------------------
        Doug Henning
        ------------------------------
  • Prashant,

    Where did you start you Jinja education?   I have been using what QB has published and a smattering of help from the Community.


    Having gone through what you and Doug have posted, it is clear I am way behind both of you.

    ------------------------------
    Don Larson
    ------------------------------
    • PrashantMaheshw's avatar
      PrashantMaheshw
      Qrew Captain

      HI Don,

      my education on Jinja is super informal , scraped together from what's available on this forum , tonnes of google effort on stack overflow, pipeline session I attended and through QuickBaseJunkie JINJA course. 

      What's helping is I'm trying to brute force solve many of problems through it , but what I achieve in many hours over the weekend , can easily be done by someone formally trained in it as I waste tonnes of time of understanding syntax. 

      I would honestly love to know have a formal intermediate/advance education in JINJA myself. 





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