Enable Dark Mode!
qweb-templates-and-operations-in-odoo-14.jpg
By: Sonu S

Qweb Templates and Operations in Odoo 14

Technical Odoo 14

QWeb is a templating engine used by odoo. It is an XML language-based templating engine and is mainly used for generating reports and pages. Qweb is also known as a report engine. Qweb template used for reporting and also for HTML fragments and pages. Using Qweb, we can customize reports according to our purposes like styling, formatting, and many more.

Here in the blog, we will discuss different types of operations in Odoo 14

The following are the different operations of the QWeb template. 

Qweb template Operations:

1. Define a variable

For defining a new variable in the Qweb template and setting a value the following syntax is used.

<t t-set=”name” t-value=”value” />

The following is an example of this Qweb template operation.

<t t-set=”test_value” t-value=”5” /> 

The output of this variable is got through the t-esc and t-raw directives. 

<t t-esc="value"/>

The result gives the value 5 and it displays on the corresponding area of the template part, t-esc is an output directive which automatically HTML escapes its content.

Here is an example, if you want to get the data from a dictionary the following code is used.   

<t t-esc="dict['key']"/>

Easly, t-esc helps us to evaluate code runtime like a python program and print it.

2. Conditional Operators

Qweb also supports conditional operations by using the t-if conditional directive. The syntax of the Qweb conditional operators is described as follows.

<div>
<t t-if=”condition”>
<operations>
</t>
</div>

The following is an example of the conditional operator in the Qweb template.

<div>
<t t-if=”timer==0”>
<t t-set=”timer” t-value=”1” />
</t>
</div>

If the condition (timer=0)  is true it prints the result 1, when the condition fails it doesn't ser value to the timer. The extra conditional branching like t-elif and t-else also can be used in the Qweb template.

Following is the example for branching directives t-if and t-else.

<div>
    <p t-if="counter==0">Idle</p>
    <p t-elif="counter==1'">Started</p>
    <p t-else="">Ongoing</p>
</div>

The counter=0 then gives the result “Idle” and when it becomes 1 it results in “Started”. If both the conditions fail, it results in “Ongoing”.

3. Loops

Qweb has an iterative directive t-foreach which is used to iterate through a set of values. It has a second parameter called t-as which details the name for the current item.


The following is an example for t-foreach iterative directive of the Qweb template.


<t t-foreach="[1, 2, 3, 4]" t-as="a">
    <p><t t-esc="a"/></p>
</t>

This will iterate over the list [1, 2, 3, 4] and print each element one by one.

4. Attributes

Qweb can compute the attribute by t-att directive which uses three methods.

Attribute t-att is $name, then the set will be rendered the attribute value as shown in the example below.

<div t-att-a="30"/>

This will be rendered as

<div a="30"></div>

If it is a key-value pair mapping, It generates new attributes and values as shown in the example below.

<div t-att="{x: 1, y: 2}"/>

 Then it rendered as 

<div x="1" y="2"></div>

If the parameter is passed as a pair then, the first value is rendered as an attribute, and the second value is taken as a value as shown in the example below.

<div t-att="['x', 'y']"/>

Then it rendered as 

<div x="y"></div>

5. Calling Other Templates

The t-call directive is used for calling other templates from the qweb template.  The following is the syntax for the t-call directive in the Qweb template.

<t t-call="other_template"/>

The other template can be defined with corresponding data. Then we can see the template other_template using this call shown in the example below.

       <template id="other_template">
        <t t-call="web.external_layout">
            <div class="page">
                <h2>
                    EXAMPLE
                </h2>
            </div>
        </t>
        </template>

These are the Qweb template and its operations in the Odoo platform.


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



0
Comments



Leave a comment

 
whatsapp
location

Calicut

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

location

London

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

location

Kochi

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

location

Bangalore

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

Send Us A Message