Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

Odoo qweb dynamic html generation

By
Paul Catinean
on 1/20/15, 2:45 AM 2,164 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?

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

Paul Catinean
on 1/20/15, 3:05 AM

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 :)

Yenthe
on 1/20/15, 3:07 AM

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

Yenthe
on 1/20/15, 3:30 AM

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

Paul Catinean
on 1/20/15, 3:35 AM

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

Leonardo Donelli
on 1/20/15, 4:54 AM

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

Sajin Aziz
on 1/20/15, 11:04 AM
1

Leonardo Donelli

--Leonardo Donelli--
1040
| 5 4 8
Cave, Italy
--Leonardo Donelli--

Physics student and part-time developer.

Leonardo Donelli
On 1/20/15, 5:13 AM

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]]

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!

Paul Catinean
on 1/20/15, 6:57 AM
4

Hardikgiri Goswami

--Hardikgiri Goswami--
1123
| 2 1 5
Rajkot, India
--Hardikgiri Goswami--

OpenERP/Odoo Customization/Integration Expert / Python Programmer

Skype ID : hardikgiri.goswami

Hardikgiri Goswami
On 1/20/15, 4:26 AM

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

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

Paul Catinean
on 1/20/15, 6:11 AM

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.

Hardikgiri Goswami
on 1/20/15, 8:20 AM
2
Sajin Aziz
On 1/20/15, 5:13 AM

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>

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!

Paul Catinean
on 1/22/15, 3:00 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 1/20/15, 2:45 AM
Seen: 2164 times
Last updated: 6/30/15, 3:21 AM