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.

1

How do you use variables in a Qweb loop?

By
Jeff Beidler
on 11/10/14, 5:01 PM 9,570 views

This page of the documentation describes variables that can be used in a t-foreach loop in Qweb, but doesn't provide any examples of how to access those variables.  I was thinking that I could use the $as_index variable to list line numbers on a sales order, but everything I have tried causes an error.  Can anyone provide an example of how to output the index?  Or, even better, the index + 1, since it starts at zero?

6
Sajin Aziz
On 11/12/14, 4:32 AM

try this code inside table,

 <t t-set="i" t-value="1"/>

<tr t-foreach="o.order_line" t-as="l">
                        <td> <span t-esc="i"/> </td>

    <t t-set="i" t-value="i+1"/>
</tr>

That's what I came up with, too. Just wanted to know how to do it in the manner described in the "documentation".

Jeff Beidler
on 11/12/14, 1:29 PM
1
Jeff Beidler
On 11/12/14, 7:18 PM

I got the $as_index variable to work.  Here is an example:

<tr t-foreach="o.order_line" t-as="l">
                        <td>
                            <span t-esc="l_index + 1" />
. . .

The index is zero-based, hence the addition of 1.  Hope this helps.

-1
Antony Lesuisse (al)
On 11/12/14, 6:41 AM

var_index is automatically defined

Check https://www.odoo.com/documentation/8.0/reference/qweb.html#loops

Yes, that is the page I linked in my question. What I was hoping for was an example of usage. Everything I tried resulted in an error.

Jeff Beidler
on 11/12/14, 1:32 PM

@Antony Lesuisse, Thanks for the link. I'm currently building a community module to add multiple product images for use in the website e-commerce pages here: https://github.com/OdooCommunityWidgets/website_multi_image unfortunately i've only just begun playing around with Odoo development and I have hit a brick wall trying to figure out how to create a t-foreach loop to output all images in the one2many in this module. Do you have any suggestions on how I could achieve this in 8.0?

Luke Branch
on 12/4/14, 6:16 AM
https://www.odoo.com/documentation/8.0/reference/qweb.html

Use the doc Luke ! :)

On 12/04/2014 12:16 PM, Luke wrote:
> @Antony Lesuisse, Thanks for the link. I'm currently building a community
> module to add multiple product images for use in the website e-commerce pages
> here: https://github.com/OdooCommunityWidgets/website_multi_image
> unfortunately i've only just begun playing around with Odoo development and I
> have hit a brick wall trying to figure out how to create a t-foreach loop to
> output all images in the one2many in this module. Do you have any suggestions
> on how I could achieve this in 8.0?
>
> --
> Luke
> Sent by Odoo S.A.  using Odoo 
> about Forum Post False 
Antony Lesuisse (al)
on 12/9/14, 4:44 PM
-1
Martin Pishpecki
On 12/24/14, 7:14 AM

I have tryed this:

<t t-set="i" t-value="1"/>
<tr t-foreach="o.order_line" t-as="l">
                        <td> <span t-esc="i"/> </td>
<t t-set="i" t-value="i+1"/>
</tr>

When the loop finishes, the ounter resets to 1. Does anyone kow a way to update a variable outside the loop? I would like to define a variable before the loop and set it to false. If I find what I am looking for inside the loop I would like to set the value of the variable to True. This var would later be used as a condition if a div is visible or not.

Any help is appreciated. Thx

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: 11/10/14, 5:01 PM
Seen: 9570 times
Last updated: 12/21/15, 8:42 AM