Skip to Content
Odoo Menú
  • Registra entrada
  • Prova-ho gratis
  • Aplicacions
    Finances
    • Comptabilitat
    • Facturació
    • Despeses
    • Full de càlcul (IA)
    • Documents
    • Signatura
    Vendes
    • CRM
    • Vendes
    • Punt de venda per a botigues
    • Punt de venda per a restaurants
    • Subscripcions
    • Lloguer
    Imatges de llocs web
    • Creació de llocs web
    • Comerç electrònic
    • Blog
    • Fòrum
    • Xat en directe
    • Aprenentatge en línia
    Cadena de subministrament
    • Inventari
    • Fabricació
    • PLM
    • Compres
    • Manteniment
    • Qualitat
    Recursos humans
    • Empleats
    • Reclutament
    • Absències
    • Avaluacions
    • Recomanacions
    • Flota
    Màrqueting
    • Màrqueting Social
    • Màrqueting per correu electrònic
    • Màrqueting per SMS
    • Esdeveniments
    • Automatització del màrqueting
    • Enquestes
    Serveis
    • Projectes
    • Fulls d'hores
    • Servei de camp
    • Suport
    • Planificació
    • Cites
    Productivitat
    • Converses
    • Validacions
    • IoT
    • VoIP
    • Coneixements
    • WhatsApp
    Aplicacions de tercers Odoo Studio Plataforma d'Odoo al núvol
  • Sectors
    Comerç al detall
    • Llibreria
    • Botiga de roba
    • Botiga de mobles
    • Botiga d'ultramarins
    • Ferreteria
    • Botiga de joguines
    Food & Hospitality
    • Bar i pub
    • Restaurant
    • Menjar ràpid
    • Guest House
    • Distribuïdor de begudes
    • Hotel
    Real Estate
    • Real Estate Agency
    • Estudi d'arquitectura
    • Construcció
    • Gestió immobiliària
    • Jardineria
    • Associació de propietaris de béns immobles
    Consulting
    • Accounting Firm
    • Partner d'Odoo
    • Agència de màrqueting
    • Bufet d'advocats
    • Captació de talent
    • Auditoria i certificació
    Fabricació
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Regals corporatius
    Salut i fitness
    • Club d'esport
    • Òptica
    • Centre de fitness
    • Especialistes en benestar
    • Farmàcia
    • Perruqueria
    Trades
    • Servei de manteniment
    • Hardware i suport informàtic
    • Solar Energy Systems
    • Shoe Maker
    • Serveis de neteja
    • HVAC Services
    Others
    • Nonprofit Organization
    • Agència del medi ambient
    • Lloguer de panells publicitaris
    • Fotografia
    • Lloguer de bicicletes
    • Distribuïdors de programari
    Browse all Industries
  • Comunitat
    Aprèn
    • Tutorials
    • Documentació
    • Certificacions
    • Formació
    • Blog
    • Pòdcast
    Potenciar l'educació
    • Programa educatiu
    • Scale-Up! El joc empresarial
    • Visita Odoo
    Obtindre el programari
    • Descarregar
    • Comparar edicions
    • Novetats de les versions
    Col·laborar
    • GitHub
    • Fòrum
    • Esdeveniments
    • Traduccions
    • Converteix-te en partner
    • Services for Partners
    • Registra la teva empresa comptable
    Obtindre els serveis
    • Troba un partner
    • Troba un comptable
    • Contacta amb un expert
    • Serveis d'implementació
    • Referències del client
    • Suport
    • Actualitzacions
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Programar una demo
  • Preus
  • Ajuda

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Comptabilitat
  • Inventari
  • PoS
  • Projectes
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
Ajuda

%if %endif does not seem to work with mail templates. Sent e-mails are blank.

Subscriure's

Get notified when there's activity on this post

This question has been flagged
emailtemplatesmailtemplateodoo8.0
8 Respostes
37803 Vistes
Avatar
Yenthe Van Ginneken (Mainframe Monkey)

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?

8
Avatar
Descartar
Avatar
Olivier Dony (odo)
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 ``<%`` and ``%>`` 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.

 

16
Avatar
Descartar
Avatar
Roland
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.

1
Avatar
Descartar
Avatar
Michael Espinosa
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

0
Avatar
Descartar
Avatar
Ossi Mantylahti
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>
            
0
Avatar
Descartar
Avatar
Victor Efimov
Best Answer

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

0
Avatar
Descartar
Avatar
Luis Triana
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.


0
Avatar
Descartar
Avatar
Jonas Hoekman
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. 


0
Avatar
Descartar
Avatar
Lithin T
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 


 

0
Avatar
Descartar
Yenthe Van Ginneken (Mainframe Monkey)
Autor

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

Lithin T

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

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Registrar-se
Related Posts Respostes Vistes Activitat
Odoo Email Template Question Solved
email format templates odoo8.0
Avatar
Avatar
2
de set. 21
6486
Email Template subject parameters Solved
email templates
Avatar
Avatar
1
de febr. 24
3611
Nothing rendered into email body_html (Odoo 16) Solved
email templates
Avatar
1
de jul. 23
4599
How to add company logo to email templates? Solved
email templates
Avatar
Avatar
Avatar
Avatar
3
de des. 23
19937
Failed to render template Solved
email templates
Avatar
Avatar
1
de febr. 22
7146
Community
  • Tutorials
  • Documentació
  • Fòrum
Codi obert
  • Descarregar
  • GitHub
  • Runbot
  • Traduccions
Serveis
  • Allotjament a Odoo.sh
  • Suport
  • Actualització
  • Desenvolupaments personalitzats
  • Educació
  • Troba un comptable
  • Troba un partner
  • Converteix-te en partner
Sobre nosaltres
  • La nostra empresa
  • Actius de marca
  • Contacta amb nosaltres
  • Llocs de treball
  • Esdeveniments
  • Pòdcast
  • Blog
  • Clients
  • Informació legal • Privacitat
  • Seguretat
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo és un conjunt d'aplicacions empresarials de codi obert que cobreix totes les necessitats de la teva empresa: CRM, comerç electrònic, comptabilitat, inventari, punt de venda, gestió de projectes, etc.

La proposta única de valor d'Odoo és ser molt fàcil d'utilitzar i estar totalment integrat, ambdues alhora.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now