Enable Dark Mode!
By: Sruthi M

Conditional and Looping in Qweb Templates


Qweb is actually a XML template engine. A template engine is a software to combine the templates with data models for creating documents. In Odoo we use qweb templates for creating reports. Qweb templates provide so many tools for developing the reports, therefore we can easily manipulate the data.
In qweb it is possible to use <t>  instead of rendering other elements. All directives in qweb are used with the prefix of t-. For example: In the case of conditionals we use t-if. 
In this blog, we will discuss conditional and looping statements in qweb templates.
We know that in the case of conditional statements we use the if directive.
    <t t-if="condition">
If the condition is true, then the output will render the element i.e.
If the condition is false, the element is removed from the result i.e.
And also we can use the t-if directly with the <p> element;
 <p t-if="condition">True</p>
In case of conditional branching directives, we use t-elif and t-else. 
For example:
    <p t-if="status === 'sent'">Sent</p>
    <p t-elif="status === 'canceled'">canceled</p>
    <p t-elif="status === 'pending">Awaiting Dispatch</p>
    <p t-else="">Error</p>
Loops :
Looping means repeating a block of code until it reaches the specified condition. Also in the case of qweb we need looping for iterating a set of values. On qweb we have lists and dictionaries. If we want to iterate each value from the list or dictionary we use t-for each. And also we use a second parameter t-as with t-foreach, t-as provides the current item in the iteration.
For eg:
 <t t-foreach="[a, b, c]" t-as="i">
    <p><t t-esc="i"/></p>
Then the result will be rendered as :
Here we are iterating a list. In the case of a dictionary, ‘i’ will be the current key.
We can also use some other variables with t-foreach and t-as . For these variables $as is replaced with the value of t-as.
By this variable, the object will be iterated again.
It is the same as $as in the case of lists and integers. But in the case of dictionaries, it will provide the value, not the key.
If the size is available, then it will take the size of the lists or dictionaries which we are iterating.
If the current item is first of the iteration that means it is equivalent to 
$as_index == 0
If the current item is the last of the iteration and it is equivalent to $as_index + 1 == $as_size. It requires the size of the iteration.
It is the parity of the current iteration, that is it will be either even or odd.
It is a boolean flag. Checks if the current iteration is an even index or not.
It is also a boolean flag. Checks if the current iteration is an odd index or not.
Example for $as_value, $as_index:
<t t-foreach="[3, 2, 1]" t-as="item">
    [<t t-esc="item_index"/>: 
    <t t-esc="item"/> 
    <t t-esc="item_value"/>]
The output will be:
[0: 3 3]
[1: 2 2]
[2: 1 1]
Example for $as_first, $as_last, $as_parity:
     <t t-foreach="5" t-as="item">
    -<t t-if="item_first"> first</t>
    <t t-if="item_last"> last</t> (
    <t t-esc="item_parity"/>)
The output will be:
- first (even)
- (odd)
- (even)
- (odd)
- last (even)
Example for $as_even, $as_odd:
	<t t-foreach="people_data" t-as="people">
                  <tr t-att-style="people_odd and 'background-color:#eeeeee' or None">
                     <td style="padding: 5px">
                         <t t-esc="people['full_name'] or ''"/>
      <tr t-att-style="people_even and 'background-color:#ffffff or None">
                     <td style="padding: 5px">
                         <t t-esc="people[email] or ''"/>
Example for $as_size:
<div t-foreach="invoice.l10n_ch_isr_optical_line" t-as="char" 
t-attf-style="right: {{ round((char_size - char_index - 1) * 0.1, 1) }}in"
The scope of these variables is inside the ‘foreach’. If in case these variables exist outside of the loop, then the value will be copied at the end of the loop into a global context.

If you need any assistance in odoo, we are online, please chat with us.


Leave a comment



Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message