record is a shortcut for widget.record and provides access to the fields available, using dot notation.
read_only_mode indicates whether the current view is in read mode (and not in edit mode).It's a shortcut for widget.view.options.read_only_mode.
A lot of books!books.
Dynamic attributes by expressions – t-att The t-att- QWeb directive dynamically generates an attribute value by evaluating an expression.
Many of the QWeb directives use expressions that are evaluated to produce some result.
A record object is available, representing the current record, with the fields requested from the server.The field values can be accessed using either the raw_value or value attributes: raw_value is the value returned by the read() server method, so it's more suitable for use in condition expressions.
value is formatted according to the user settings and is meant to be used for display in the user interface.This is typically relevant for date/datetime, float/monetary, and relational fields.
instance is a reference to the full web client instance.
It's also noteworthy that some characters are not allowed inside expressions.The lower than sign ( have special meaning and shouldn't be used on the XML content.A negated >= operator is a valid alternative, but the common practice is to use the following alternative symbols that are available for inequality operations: lt is for less than.
lte is for less than or equal to.
gt is for greater than.
gte is for greater than or equal to.
The preceding comparison symbols are specific to Odoo and were introduced to overcome limitations in the XML format.They are not part of the XML standard.
Dynamic attributes by string substitution – t-attf Our Kanban card is using the t-attf QWeb directive to dynamically set a class on the top element so that the card is colored depending on the color field value.For this, the t-attf- QWeb directive was used.
The t-attf- directive dynamically generates tag attributes using string substitution.This allows for parts of larger strings generated dynamically, such as a URL address or CSS class names.
The directive looks for expression blocks that will be evaluated and replaced by the results.
As an elaborate example, we'll use this directive to dynamically change the color of the user, to be in red font if the priority is high.For this, replace in our Kanban card with the following: This results in either class="oe_kanban_text_red" or class="oe_kanban_text_black", depending on the checkout's priority value.Please note that, while the oe_kanban_text_red CSS class is available in Kanban views, the oe_kanban_text_black CSS class does not exist and was used to explain the point.
Our Kanban card uses it to dynamically set some attributes on the tag; the title attribute is dynamically rendered using the following: t-att-title="record.member_id.value" The .value field returns its value representation as it should be shown on the screen.For many-to-one fields, this is usually the related record's name value.For users, this is the username.As a result, when hovering the mouse pointer over the image, you will see the corresponding username.
When the expression evaluates to a false equivalent value, the attribute is not rendered at all.This is important for special HTML attributes such as the checked input field, which can have an effect even without an attribute value.
Loops – t-foreach A block of HTML can be repeated by iterating through a loop.We can use it to add the avatars of the record followers.
The t-esc directive used next evaluates the provided expression, just the rec variable name in this case, and renders it as safely escaped HTML.
As an example, to only display the checkout's number of books borrowed if it has a value, add the following after the request_date field: books We used a element so that if the condition is false, the element produces no output.If it's true, only the contained element is rendered to the output.
Notice that the condition expression used the gt symbol instead of > to represent the greater than operator.
The else if and else conditions are also supported with the t-elif and t-else directives.Here is an example of their usage: No books.
.But the ampersand symbol is not allowed in XML.We can work around this using the and and or operators.
Rendering values – t-esc and t-raw We used the element to render the field content.But field values can also be presented directly without a tag.
The t-esc directive evaluates an expression and renders it as an HTML-escaped value, as shown in the following: In some cases, and if the source data is guaranteed to be safe, t-raw can be used to render the field raw value without any escaping, as shown in the following example: For security reasons, it's important to avoid using t-raw as much as possible.Its usage should be strictly reserved for outputting HTML data that was specifically prepared without any user data in it or where any user data was escaped explicitly for HTML special characters.
Set values on variables – t-set For more complex logic, we can store the result of an expression into a variable to use it later in the template.This is to be done using the t-set directive, naming the variable to set followed by the t-value directive, with the expression calculating the value to assign.
As an example, the following code renders missed deadlines in red, just as in the previous section, but uses a red_or_black variable for the CSS class to use, as shown in the following: t-value=" record.priority.raw_value gte '2' ?'oe_kanban_text_red' : ''" /> Variables can also be assigned HTML content, as in the following example: Call and reuse other templates – t-call QWeb templates can be reusable HTML snippets that can be inserted into other templates.
Instead of repeating the same HTML blocks over and over again, we can design building blocks to compose more complex user interface views.
Reusable templates are defined inside the tag and identified by a top element with a t-name other than kanban-box.These other templates can then be included using the t-call directive.This is true for the templates declared in the same Kanban view, somewhere else in the same add-on module or in a different add-on.
The follower avatar list is something that could be isolated in a reusable snippet.Let's rework it to use a sub-template.We should start by adding another template to our XML file, inside the element, after the node, as shown in the following: t-as="rec"> class="oe_avatar" width="24" height="24" /> Calling it from the kanban-box main template is quite straightforward.Instead of the element containing the for each directive, we should use the following: To call templates defined in other add-on modules, we need to use the module.name full identifier, as we do with the other views.For instance, this snippet can be referred using the library_checkout.follower_avatars full identifier.
The called template runs in the same context as the caller, so any variable names available in the caller are also available when processing the called template.
A more elegant alternative is to pass arguments to the called template.This is done by setting variables inside the t-call tag.These will be evaluated and made available in the sub-template context only and won't exist in the caller context.
We could use this to have the maximum number of follower avatars set by the caller instead of being hardcoded in the sub-template.First, we need to replace the fixed value, 3, with a variable, arg_max, example: t-as="rec"> class="oe_avatar" width="24" height="24" /> Then, define that variable's value when performing the sub-template call as follows: The entire content inside the t-call element is also available to the sub-template through the 0 magic variable.Instead of argument variables, we can define an HTML code fragment that can be used in the sub-template with .This is especially useful for building layouts and combining/nesting QWeb templates in a modular way.