[QWeb2] t-if, t-elseif, t-else

Bug #1156830 reported by Daniel Hammerschmidt
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Odoo Web (MOVED TO GITHUB)
Confirmed
Wishlist
OpenERP R&D Web Team

Bug Description

For QWeb2-templated I would like to have elseif- and else-directives instead of a couple of if-directives where each and every is evaluated to test for any combination of operands.

In base.res_partner_kanban_view we have an unordered list for parent_id, function, city, country and email:
[code]
<ul>
    <li t-if="record.parent_id.raw_value and !record.function.raw_value"><field name="parent_id"/></li>
    <li t-if="!record.parent_id.raw_value and record.function.raw_value"><field name="function"/></li>
    <li t-if="record.parent_id.raw_value and record.function.raw_value"><field name="function"/> at <field name="parent_id"/></li>
    <li t-if="record.city.raw_value and !record.country.raw_value"><field name="city"/></li>
    <li t-if="!record.city.raw_value and record.country.raw_value"><field name="country"/></li>
    <li t-if="record.city.raw_value and record.country.raw_value"><field name="city"/>, <field name="country"/></li>
    <li t-if="record.email.raw_value"><a t-attf-href="mailto:#{record.email.raw_value}"><field name="email"/></a></li>
</ul>
[/code]

It would be more expressive and easier to manage for more complex expressions this way:
[code]
<ul>
  <li t-if="record.parent_id.raw_value">
    <t t-if="record.function.raw_value"><field name="function"/> at </t><field name="parent_id"/>
    <li t-elseif="record.function.raw_value"><field name="function"/></li>
  </li>

  <li t-if="record.city.raw_value">
    <field name="city"/><t t-if="record.country.raw_value">, <field name="country"/></t>
    <li t-elseif="record.country.raw_value"><field name="country"/></li>
  </li>

  <li t-if="record.email.raw_value"><a t-attf-href="mailto:#{record.email.raw_value}"><field name="email"/></a></li>
</ul>
[/code]

See attachment for my proposal.

Tags: qweb2
Revision history for this message
Daniel Hammerschmidt (redneck) wrote :
Revision history for this message
Amit Bhavsar (Open ERP) (amb-openerp) wrote :

Hello Daniel,

It's really nice Amelioration, But this is not a bug but rather than it's your suggestion to Improve the code of qweb2.js for manage complex expressions.

We can also Improve this code with out change qweb2.js in following way...
<li t-if="record.city.raw_value or record.country.raw_value">
    <field name="city"/>
        <t t-if="record.city.raw_value and record.country.raw_value">,</t>
    </t><field name="country"/>
</li>

But.......... ... ..
It's good Implementation as per the coding conventions point of view. So we can consider this is as your suggestion and considering it as a "Wishlist". and Let the team to take decision on it.

@Antoine (al) : please give look.

Thanks for the reporting!

Changed in openerp-web:
status: New → Confirmed
importance: Undecided → Wishlist
assignee: nobody → OpenERP R&D Web Team (openerp-dev-web)
Revision history for this message
Daniel Hammerschmidt (redneck) wrote :
Download full text (3.3 KiB)

Hello Amit,

thank you for you comment.

I know, it is not a bug and the wishlist is what I have expected.

Currently I am "playing around" with partners, crm and the new great web-client. I wanted to have different avatars for non-company-partners of type 'contact' and other non-company-partners (a human head next to a delivery address looks somehow wrong) in the kanaban-view.

Following the whole thing:
[code]
<record id="view_partner_kanban" model="ir.ui.view">
  <field name="inherit_id" ref="base.res_partner_kanban_view" />
  <field name="name">res.partner.kanban.names</field>
  <field name="model">res.partner</field>
  <field name="arch" type="xml">
    <data>
      <xpath expr="/kanban/field[1]" position="after">
        <field name="type"/>
      </xpath>
      <xpath expr="/kanban/templates//a[@type='open'][1]/t[2]/t[2]/img" position="replace">
        <a type="open">
          <t t-if="record.has_image.raw_value === true">
            <img t-att-src="kanban_image('res.partner', 'image_small', record.id.value)" class="oe_kanban_image"/>
            <img t-elseif="record.is_company.raw_value === true" t-att-src='_s + "/base/static/src/img/company_image.png"' class="oe_kanban_image"/>
            <img t-elseif="record.type.raw_value === 'contact'" t-att-src='_s + "/base/static/src/img/avatar.png"' class="oe_kanban_image"/>
            <img t-else="" t-att-src='_s + "/base/static/src/img/icon.png"' class="oe_kanban_image"/>
          </t>
        </a>
      </xpath>
      <xpath expr="/kanban/templates//ul" position="replace">
        <ul>
          <li t-if="record.parent_id.raw_value">
            <t t-if="record.function.raw_value"><field name="function"/> <i>at</i> </t><field name="parent_id"/>
            <li t-elseif="record.function.raw_value"><field name="function"/></li>
          </li>
          <li t-if="record.city.raw_value">
            <field name="city"/><t t-if="record.country_id.raw_value">, <field name="country_id"/></t>
            <li t-elseif="record.country_id.raw_value"><field name="country_id"/></li>
          </li>
          <li t-if="record.email.raw_value"><a t-attf-href="mailto:#{record.email.raw_value}"><field name="email"/></a></li>
        </ul>
      </xpath>
    </data>
  </field>
</record>
[/code]

As you can see there are two forms to use this directives:
1.
<div t-if>
  "if-content"
  <div t-elseif/>
  "elseif-content"
  <div t-else/>
  "else-content"
</t-if>

2.
<t t-if>
  <div>"if-content"</div>
  <t t-elseif>
    <div>"elseif-content"</div>
  </t>
  <t t-else>
    <div>"else-content"</div>
  </t>
</t-if>

Both are compiled to something like that
if (cond) {
  r.push('<div>"if content"</div>')
} else if (cond) {
  r.push('<div>"elseif- content"</div>')
} else {
  r.push('<div>"else- content"</div>')
}

It might be confusing for a reviewer if both forms are mixed within one if-elseif-else-compound.
In the second form content between the last closing </t> and the closing </t-if> is appended to the else-content.

Solution 1: Well documentation and responsibility of the developer who designs the view.
Solution 2: QWeb2 raises an exception on malformed usage (first form only for non-t-tags second form on...

Read more...

Revision history for this message
Daniel Hammerschmidt (redneck) wrote :

Sorry, the correct first form is as follows.
<div t-if>
  "if-content"
  <div t-elseif>"elseif-content"</div>
  <div t-else/>"else-content"</div>
</div>

Revision history for this message
Daniel Hammerschmidt (redneck) wrote :

Hello Amit,

I created a branch for this issue.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.