python-docx-template
python-docx-template copied to clipboard
Nested tables in normal for loops
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
You have not given the whole data, but context.data_details requires there is another outer nested for-loop isn't it ?
You have not given the whole data, but
context.data_detailsrequires 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 %}
could you try without the dash in {%- endfor %}