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.
Conditionals:
We know that in the case of conditional statements we use the if directive.
<div>
<t t-if="condition">
<p>True</p>
</t>
</div>
If the condition is true, then the output will render the element i.e.
<div>
<p>True</p>
</div>
If the condition is false, the element is removed from the result i.e.
<div>
</div>
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:
<div>
<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>
</div>
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>
</t>
Then the result will be rendered as :
<p>a</p>
<p>b</p>
<p>c</p>
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.
$as_all
By this variable, the object will be iterated again.
$as_value
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.
$as_size
If the size is available, then it will take the size of the lists or dictionaries which we are iterating.
$as_first
If the current item is first of the iteration that means it is equivalent to
$as_index == 0
$as_last
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.
$as_parity
It is the parity of the current iteration, that is it will be either even or odd.
$as_even
It is a boolean flag. Checks if the current iteration is an even index or not.
$as_odd
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"/>]
</t>
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"/>)
</t>
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 ''"/>
</td>
</tr>
<tr t-att-style="people_even and 'background-color:#ffffff or None">
<td style="padding: 5px">
<t t-esc="people[email] or ''"/>
</td>
</tr>
</t>
Example for $as_size:
<div t-foreach="invoice.l10n_ch_isr_optical_line" t-as="char"
t-esc="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.