python-docx-template icon indicating copy to clipboard operation
python-docx-template copied to clipboard

Nested tables in normal for loops

Open derekhsu opened this issue 1 year ago • 3 comments

Describe your problem

I got a error message as the following lines when I put {%tr ... %} tag inside a normal for loops:

Encountered unknown tag 'tr'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.

More details about your problem

My template

{% for data_detail in context.data_details %}
  {{ data_detail.data_name }}
 ----------------------------------------
 | Field          | Description   |  Remark|
 | {%tr for data_field in data_detail.data_fields %} |
| ---------------------------------------- |
 | {{ data_field.field }}  | {{data_field.description}} | {{data_field.remark}} |
| ---------------------------------------- |
| {%tr endfor %}
| ---------------------------------------- |
{%- end for %}

Provide a test case

My context is listed below:

"contexts": [
...
"data_details": [
                {
                    "data_name": "DI List frame",
                    "data_fields": [
                        {
                            "field": "DI No.",
                            "description": "DI No",
                            "remark": "Display field"
                        },
                        {
                            "field": "Picking Slip No.",
                            "description": "Picking Slip No.",
                            "remark": "Display field"
                        }
                    ]
                },
...

Screenshots

image

derekhsu avatar Sep 27 '24 03:09 derekhsu

You have not given the whole data, but context.data_details requires there is another outer nested for-loop isn't it ?

elapouya avatar Sep 27 '24 08:09 elapouya

You have not given the whole data, but context.data_details requires there is another outer nested for-loop isn't it ?

Original data is too large to give, so I ignore it. It looks like

context = { 
    "contexts": [
        {
            "heading": "Picking Slip Production",
            "modules": ["shjjL0400.jsp", "~rest.ssjcRestL0400"],
            "instructions": [
            ,,,
            ]
        },
        {
           "heading": "...",
           ,,,
        }
     ]

So there is a normal for loop outer of the table, such as

{% for context in contexts %}
...
{% endfor %}

derekhsu avatar Sep 27 '24 09:09 derekhsu

could you try without the dash in {%- endfor %}

elapouya avatar Sep 27 '24 22:09 elapouya