This question has been flagged
15 Replies
12487 Views

Hello everyone

I wanted to know how does one handle a dynamically generated xml code that is invalid before being proccessed 

For example generating a table that displays the information in two cells per row:

<t t-foreach="list" t-as="item">

    <tr t-if="item_even">

        <td><span t-field="item"></td>

    </tr t-if="item_odds>

</t>

This will not work since it's invalid xml because the closing tag has a property and neither will <t t-if="condition"> </tr> </t> since the closing tag is embeded in what the xml sees another tag

Alternatives to this would be looping over grouped items, two by two or how many you want per column, and placing them inside one <tr> in one loop but it's not that elegant

Any way around this?

Avatar
Discard
Author

That is the purpose of the var_last var_first and var_odds var_even (in your case i_last, i_first) to hold index information see: http://odoo-80.readthedocs.org/en/latest/reference/qweb.html#loops The logic of achieving this is not the problem it's just that it does not work in qweb.You need two in the same to make a two columned table. This means you have to open on even and close on odds so the end result is 1 2

Hmm I see I understood your question wrong so I've deleted my reply as it wasn't of any value then. I'm not sure about this though, interesting question. Upvoted it :)

Have a look at this by the way: http://odoo-80.readthedocs.org/en/latest/reference/qweb.html#attributes

Author

My theory (though could very well be wrong) is that the error comes in even before qweb can kick into action.If qweb would come in and resolve the t-tags and loops it would result in valid xml code But before the qweb is parsed it's actually read as standard xml where it sees a closing tag having a attribute which is invalid syntax and stops dead.It does not know that the attribute is actually a qweb condition that will output a empty closing tag which will in the end compose valid XML

IMO looping on a grouped list it's the correct way and a lot more elegant than a conditional closing tag.

is this what you are trying for (last answer) , it works in qweb reports.

Best Answer

You say:

Alternatives to this would be looping over grouped items, two by two or how many you want per column, and placing them inside one <tr> in one loop but it's not that elegant

But I don't think that's true at all. Conceptually, it better represents what you are trying to do, while the conditional closing tag feels more like an hack.

Also notice that your code would not generate valid XML if the number of items is odd (it would never close the last <tr> tag), and generally that's the problem with what you wanted to do: you cannot be sure that the generated html will be valid after evaluation because it depends on the context.

To create a grouped list with groups of size n:

[ l[i:i+n] for i in xrange(0, len(l), n) ]

Examples:  

 l = range(7)
>>> [ l[i:i+2] for i in xrange(0, len(l), 2) ]
[[0, 1], [2, 3], [4, 5], [6]]
>>> [ l[i:i+3] for i in xrange(0, len(l), 3) ]
[[0, 1, 2], [3, 4, 5], [6]]
>>> [ l[i:i+4] for i in xrange(0, len(l), 4) ]
[[0, 1, 2, 3], [4, 5, 6]]

Avatar
Discard
Author

While indeed it is a limitation you cannot do conditional closing tags your explanation makes a lot of sense and it is the proper/better method of tackling this issue regardless of the templating system.Thank you for your complete and eloquent answer!

Best Answer

try This :

 <tr>
            <t t-foreach="list" t-as="item">

                    <td><span t-esc="item_index"/></td>
                    <t t-if="item_odd">
                        &lt;/tr &gt;
                        &lt;tr &gt;
                    </t>
            </t>

</tr>

Avatar
Discard
Author

Sajin indeed this solution works and provides the possibility of doing conditional closing tags i.e parsing xml that would otherwise be invalid.Thank you for your help!

Actually this is the only and complete solution on the question above. I can confirm that it works now as well. Grouping is indeed better in some of the solutions - but somtimes - grouping is bad idea as well. Consider the "one data source" for the lets say select field. If you want to implement behaviour based on the data source - and present that select field as well on the frontend, then its much easier to have one object instead grouped one.

Best Answer

Hello Paul,

As I observed you theory is on opposite way of qweb tag (specially for the ending tag).

But I can understand what kind of output you want.

You need to create list of list.

I am explaining by your example

<t t-foreach="list" t-as="item">

    <tr t-if="item_even">

        <td><span t-field="item"></td>

    </tr t-if="item_odds>

</t>

Here "list" is a list something like [1,2,3,4,5].
and it will print like 
1
2
3
4
5

and you want it like
1   2
3   4
5

If you want something like this, then you need to pass list of list like,
your "list" should be like [[1,2],[3,4],[5]] instead of [1,2,3,4,5].
Now you xml code should be something like.

<t t-foreach="list" t-as="row">

    <tr>
        <t t-foreach="row" t-as="item">

            <td><span t-field="item"></td>
        </t>
    </tr>

</t>

and python code for converting simple "list" this "list of list" is

Option 1 :


your_list = [1,2,3,4,5,6,1,2,3,4,5,6]
new_list = []
for i in range(0, len(your_list), 2):
    temp_list = [your_list[i]]
    if i < len(your_list)-1:
        temp_list.append(your_list[i+1])
    new_list.append(temp_list)
print new_list

Option 2 :

your_list = [1,2,3,4,5,6,1,2,3,4,5,6]
new_list1 = zip(*[iter(your_list)]*2)
print new_list1

Avatar
Discard
Author

Indeed Hardikgiri that is the alternative when looping through a grouped set

As per my knowledge solution for this question is not possible. I have tried two other ways but I got the error and stuck over there.

Best Answer

I have a similar requirement but with three columns for a pay slip report. 

The solution that I came up with is to construct some part of the Qweb report in python and save the generated html in a text field.


Call the text field in the Qweb report like this:

<div t-field="o.field_name" t-options="{'widget':'html'}"/>


When the Qweb report gets generated, the content of the text field is rendered together with the rest of the report. I use this approach in Odoo13 but I guess will work on previous versions as well.

Avatar
Discard