This question has been flagged
8 Replies
33567 Views

Why aren't %if %endif statements working in Odoo V8?

When you create a new e-mail template (under 'Marketing' > 'Mail templates' and when you then click on 'Edit template' you could add %if %endif statements. 
For example:

Dear ${(object.name or 'Sir / Madam')|safe} Thank you for registering.. We have these details: % if object.street: ${(object.street or '-') | safe} % endif

However, when you save this template and preview it you'll see it is not working. If I send the e-mail to the person it will be empty like this:

The content isn't printed and it seems that %if %endif ruins the whole e-mail.

Could anybody explain me why this is not working and what I'm doing wrong?

Avatar
Discard
Best Answer

When a non-empty [email] template is rendered blank, it means the rendering engine failed to parse the template.

This parsing problem is likely caused by a combination of factors:

  • The `%` syntax in Jinja2/Mako templates corresponds to line statements, and this only works as soon as the % sign is the first non-blank character on the line.
  • When you're dealing with HTML templates (as is the case for email templates), there's a very very good chance every line has a lot of invisible HTML tags before the % sign, or that the % sign is not on its own line.

To convince yourself, switch to the source view of your template (the rightmost icon in the HTML editor toolbar, looking like `[<>]`), and you'll see the mixup.

This is why it works when you change the templates in the XML source files of Odoo directly, as you're dealing with the raw HTML source in that case.

Solution: use the source view to write HTML directly, where you can use the full Jinja2 syntax including line statements. Some advanced users might also want to permanently switch to a plaintext version of the template body, by editing the form view of mail templates and forcing `widget="text"` for the `body_html` field. This is safer if you do advanced things, because the HTML editor could also reject/mess up templates that do not contain valid HTML, for example with % line statements in between `<tr>` or `<td>` elements in a table.
You could also try to use the ```` markers for block statements, but that will definitely fail with the HTML editor (even in source mode) because the angle brackets will be auto-escaped, being invalid HTML tags.

One more possible issue: watch out that you cannot use variables expressions on the line statement itself,  another common mistake that seems present in the issue description... 

This is invalid:
  % if object.street2: ${(object.street or '-') | safe}
  % endif

While this is valid (even if quite strange, mixing 2 different fields):
  % if object.street2:
      ${(object.street or '-') | safe}
  % endif

If you're not sure, check the Jinja2 documentation.

 

Avatar
Discard
Best Answer

We are having a similar issue in Odoo v9. Modifying the templates without being aware of the tag requirements can result in this error. However, editing the mail template again and using the 'code' view in the content editor to reinstate the default system template did not fix it. We compared this template to a fresh install and there was no difference.

In the end, the only thing that worked was finding the corresponding entry in the PostgreSQL database and reverting that back to the system default. Suddenly it works fine.

It certainly seems from here that the Odoo editor features for mail templates will break the formatting no matter how you submit them (wysiwyg or code view). I think it should be considered a bug.

Avatar
Discard
Best Answer

Hi I am on version 15 of odoo and the way to use IF conditions is with
And inside the t you write what you want to do

Avatar
Discard
Best Answer

Updating a bit for this original question for Odoo 13. The issue is still present and can easily cause total e-mail template rendering failing if one evaluation fails. We ran into this when modifying Spanish language version of the offer e-mail template. A typical use case is to check if a field has content or not before printing it out to the e-mail template as fields without any content set to them evaluate as text "False".

In our case we wanted to print tittle of the recipient if he has one ("Dear doctor Stephen Strange" if Stephen Strange has tittle defined). The field is object.partner_id.title.name and to check if it is filled, the code is following:

% if object.partner_id.title.name:
 ${object.partner_id.title.name}
% endif


So the whole block becomes:


<div style="font-size:13px;font-family:&quot;Lucida Grande&quot;, Helvetica, Verdana, Arial, sans-serif;margin:0px;padding: 0px;">
    <p style="margin:15px;font-size:13px;font-family:&quot;Lucida Grande&quot;, Helvetica, Verdana, Arial, sans-serif;padding: 2px; font-size: 13px;">
% set doc_name = 'oferta' if object.state in ('draft', 'sent') else 'orden'
        Estimado  
% if object.partner_id.title.name:
 ${object.partner_id.title.name}
% endif

${object.partner_id.name},
        <br><br>
        Su
% if ctx.get('proforma'):
            Pro forma factura ${doc_name} <strong style="font-weight:bolder;">${object.name}</strong>
% if object.origin:
                (with reference: ${object.origin} )
% endif
            con el precio total de <strong style="font-weight:bolder;">${format_amount(object.amount_total, object.pricelist_id.currency_id)}</strong> está disponible.
% else:
            ${doc_name} <strong style="font-weight:bolder;">${object.name}</strong>
% if object.origin:
                (with reference: ${object.origin} )
% endif
            con el precio total de <strong style="font-weight:bolder;">${format_amount(object.amount_total, object.pricelist_id.currency_id)}</strong> está listo para su revisión.
% endif
        <br><br>
        No dude en contactarnos si tiene alguna pregunta.
        <br><br></p>
    <p style="margin:15px;font-size:10px;font-family:&quot;Lucida Grande&quot;, Helvetica, Verdana, Arial, sans-serif;padding: 2px; font-size: 8px;">
Términos y condiciones generales: ...Terms and conditions here...
    </p>
</div>
            
Avatar
Discard
Best Answer

Oh, through years I've stumbled on this issue again. Thank for sharing this remedy!

Avatar
Discard
Best Answer

There is an issue there the %if &endif is not working, i don't know why, the way i solved was this:

I did it in the mail template for sale.order:

     Order reference: ${(object.origin or 'No Origin Document'}

Hope this works in your mail template.


Avatar
Discard
Best Answer

I have almost the same problem the template is just not fully parsed, I have edited the default invoice and sales order templates and need to be able to restore these back to the original ones, on a development server I was able to do this by removing the templates and update the module account_voucher, but on my production server I have always 504 Gateway Time-out (nginx)

have tried another way by exporting these both the templates and the Dutch (BE)/ Nederlands (BE) translations on a new server installation and import it on the production server but seems not to work. 


Avatar
Discard
Best Answer

It should work if the fields are specifed are in the object you selected.
see the below sample applied to sale.order object which %if %endif works perfect.

  Order number: ${object.name}

  Order total: ${object.amount_total} ${object.pricelist_id.currency_id.name}

  Order date: ${object.date_order}
% if object.origin:   Order reference: ${object.origin}
% endif % if object.client_order_ref:   Your reference: ${object.client_order_ref}
% endif % if object.user_id:   Your contact: ${object.user_id.name} % endif 


 

Avatar
Discard
Author

Thanks for your answer Lithin. Did you create a new template for mass mailing and through the webinterface though? This shouldn't work as it is even confirmed by the Odoo developers. This question was made to intentionally be answered by one of the Odoo developers & to promote it to the docs, as you can see in this Github report which explains everything: https://github.com/odoo/odoo/issues/4671

Yes Yenthe,I confirmed. through webinterface it is not working fine as expected.