basic-qweb-operations.png
Blogger_636313952775783385.jpg
By: Linto

Basic Qweb Operations

Qweb is a templating engine or reporting engine which can be used to create reports. Odoo uses Qweb for generating reports. Qweb provides several tools for creating a report. By using Qweb, we can manipulate the data very easily.

We will discuss the basic features available in Qweb for creating a report. We use XML attributes prefixed by ‘t-’ for performing the operations.
Just like any other programming language, we can define a new variable, assign a value to it, and use it in our template. 

How to define a variable

t-set  can be used to define a new variable and set its value.

Syntax is,

	<t t-set=”variable_name” t-value=”value of the variable” />

Example: 

	<t t-set=”count” t-value=”0” />   

This will create a new variable ‘count’ and set its value to zero. We can set the value of the value of this variable using a function. 

		<t t-set=”count” t-value=”my_function()” /> 

Here, the value of ‘count’ will be the value returned from the function ‘my_function’. When we define a variable, we don’t need to specify a specific type of this variable. We can have several types of values such as numbers, strings, arrays(lists) and dictionaries etc. If we need to define a dictionary, we can achieve it like this,

	<t t-set=”my_dict” t-value=”{'my': 'first', 'my2': 'second' }” /> 

Just like this, we can use lists also. Iterating over the different values of a list or dictionary can be achieved by using a loop.

Output

In order to output a value, qweb provides t-esc and t-raw directives. The commonly used one is t-esc.

	<t t-esc="value"/>

t-esc will evaluate the variable and print the content of that variable. The difference between t-esc and t-raw is, t-esc will automatically HTML-escape its content while t-raw won’t.

Conditional Operators

After setting a variable, we can perform our operations on that variable. We can use t-if to perform any conditional operations. The basic syntax is as follows. 

	<t t-if=”condition”> <do your operations></t>


Example: 

	<t t-if=”count==0”><t t-set=”count” t-value=”1” /></t>
This will first check the value of ‘count’ is zero or not. If yes, the contents inside the t-if will be executed.

The ‘else’ condition can be achieved by using a ‘not’ before the condition in the t-if tag. The ‘not’ can be used to reverse the condition. i.e just like the else condition.

Example:

		<t t-if=”not count==0”><t t-set=”count” t-value=”1” /></t>

If we need to include multiple conditions, we can achieve this by using ‘and’ or ‘or’. 

i.e,  
	<t t-if=”not (count==0 or count ==1)”><t t-set=”count” t-value=”1” /></t>

Loops
In some cases, we may need to iterate over a set of values. Suppose we have a list or a dictionary with us and we need to iterate through each value, in this case, we can use t-for each for this purpose.

Example:

	<t t-foreach=”[1,2,3]” t-as=”i”>
<t t-esc=”i” /> 
</t>

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


Calling Other Templates

Calling other templates from qweb can be achieved using the t-call directive. This can be used for top-level rendering or within a template.
	<t t-call="other_template"/>

Handling Fields

When we are handling fields, we can use t-field directive ( the result of the browser method). t-field-options can be used for customizing these fields. The widget is one of the most common options used with t-field options. We can have different types of widgets for different types of values.




Comments

0


Leave a comment