Using the loop variable in header
Hi, guy I want to generate a document with different header text and my exmaple data is : data = [{hdr:"a"},{hdr:"b"},{hdr:"c"}]
My example template contains three pages: page 1: no header and tranverse the data using "{% for item in data %}" page 2: its header is "{{ item.hdr }}" page 3: no header and end for loop with "{% endfor %}"
when I run this exmple, it reports a error " hdr of undefined". So how can i revise it, anyone can help me? thanks.
Declare your list named data like this, [{"hdr" : "a"}, {"hdr" : "b"}, {"hdr" : "c"}]
@Tanishqmalu Sorry I didn't format the data type on web corrently. My local data is as you listed. Thanks.
@PH8919 Probably
{{ item["hdr"] }}
is correct syntax to access key of dictionary?
@PH8919 Probably
{{ item["hdr"] }}is correct syntax to access key of dictionary?
Hi, guy
I think my syntax is correct, you can refer to jinja2 tutorial.
Thanks
Hi, guys
I think it is because the header and footer are in defferent files for XML and rendered firstly. But the for loop is in written in document file. So when rendering the header and footer, it can access the loop variables. Fortunately, I found a alternative method, user can render the whole document in different DOCX files with separate chapter, and you can promote the header/footer data to be global. then using the docxcomposer to compose the separate documents.
Thanks