Search code examples
htmlxmlodooqweb

Odoo 14 render template with model records


I am new to the QWeb template engine, and I am trying to use the attributes of a model record that I passed via the request.render function for the class name.

For example:

In my Web Controller, I respond with:

return request.render('sync.product_sync', {'odoo_products': products_odoo})

Now, I want to insert the id attribute of every >products_odoo< in the class tag of a div:

<t t-foreach="odoo_products" t-as="odoo_prod">
    <div class="odoo_prod_... {{ odoo_prod.id }}"> <t t-esc="odoo_prod.name"/> </div>
</t>

results in

<div class="odoo_prod_... {{ odoo_prod.id }}"> Productname 1 </div>
<div class="odoo_prod_... {{ odoo_prod.id }}"> Productname 2 </div>
<div class="odoo_prod_... {{ odoo_prod.id }}"> Productname 3 </div>
<div class="odoo_prod_... {{ odoo_prod.id }}"> Productname 4 </div>
... 
...

I just found that _index syntax that not given me the result I want...

Thanks


Solution

  • It's because you add class= but Odoo will process on attribute only when the attribute is prefixed by t-attf-$name.

    In your case is t-attf-class="odoo_prod_... {{ odoo_prod.id }}"

    More information

    Example:

    <t t-foreach="[1, 2, 3]" t-as="item">
        <li t-attf-class="row {{ (item_index % 2 === 0) ? 'even' : 'odd' }}">
            <t t-esc="item"/>
        </li>
    </t>