Conditionals – t-if

Our Kanban view used the t-if directive in the card option menu to make some options available depending on some conditions. The t-if directive expects an expression to be evaluated in JavaScript when rendering Kanban views on the client side. The tag and its content will be rendered only if the condition evaluates to true.

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:

<t t-if="record.num_books.raw_value gt 0"> 
  <li> <field name="num_books"/> books</li> 
</t> 

We used a <t t-if="..."> element so that if the condition is false, the element produces no output. If it's true, only the contained <li> 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:

<t t-if="record.num_books.raw_value == 0"> 
  <li>No books.</li> 
</t>
<t t-elif="record.num_books.raw_value gt 9">
<li>A lot of books!</li>
</t>
<t t-else="">
<li> <field name="num_books"/> books.</li>
</t>

In Javascript expressions, the AND and OR operators are && and ||. But the ampersand symbol is not allowed in XML. We can work around this using the and and or operators.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.57.222