Skip to Content
Odoo Menu
  • Prihlásiť sa
  • Vyskúšajte zadarmo
  • Aplikácie
    Financie
    • Účtovníctvo
    • Fakturácia
    • Výdavky
    • Tabuľka (BI)
    • Dokumenty
    • Podpis
    Predaj
    • CRM
    • Predaj
    • POS Shop
    • POS Restaurant
    • Manažment odberu
    • Požičovňa
    Webstránky
    • Tvorca webstránok
    • eShop
    • Blog
    • Fórum
    • Živý chat
    • eLearning
    Supply Chain
    • Sklad
    • Výroba
    • Správa životného cyklu produktu
    • Nákup
    • Údržba
    • Manažment kvality
    Ľudské zdroje
    • Zamestnanci
    • Nábor zamestnancov
    • Voľné dni
    • Hodnotenia
    • Odporúčania
    • Vozový park
    Marketing
    • Marketing sociálnych sietí
    • Email marketing
    • SMS marketing
    • Eventy
    • Marketingová automatizácia
    • Prieskumy
    Služby
    • Projektové riadenie
    • Pracovné výkazy
    • Práca v teréne
    • Helpdesk
    • Plánovanie
    • Schôdzky
    Produktivita
    • Tímová komunikácia
    • Schvalovania
    • IoT
    • VoIP
    • Znalosti
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Managament
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Výroba
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware and Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Komunita
    Vzdelávanie
    • Tutoriály
    • Dokumentácia
    • Certifikácie
    • Školenie
    • Blog
    • Podcast
    Empower Education
    • Vzdelávací program
    • Scale Up! Business Game
    • Visit Odoo
    Softvér
    • Stiahnuť
    • Porovnanie Community a Enterprise vierzie
    • Releases
    Spolupráca
    • Github
    • Fórum
    • Eventy
    • Preklady
    • Staň sa partnerom
    • Services for Partners
    • Register your Accounting Firm
    Služby
    • Nájdite partnera
    • Nájdite účtovníka
    • Meet an advisor
    • Implementation Services
    • Zákaznícke referencie
    • Podpora
    • Upgrades
    ​Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Získajte demo
  • Cenník
  • Help

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

  • CRM
  • e-Commerce
  • Účtovníctvo
  • Sklady
  • PoS
  • Projektové riadenie
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tagy (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tagy (View all)
odoo accounting v14 pos v15
About this forum
Pomoc

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

Odoberať

Get notified when there's activity on this post

This question has been flagged
emailtemplatesmailtemplateodoo8.0
8 Replies
37765 Zobrazenia
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
Zrušiť
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
Zrušiť
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
Zrušiť
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
Zrušiť
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
Zrušiť
Avatar
Victor Efimov
Best Answer

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

0
Avatar
Zrušiť
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
Zrušiť
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
Zrušiť
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
Zrušiť
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!

Registrácia
Related Posts Replies Zobrazenia Aktivita
Odoo Email Template Question Solved
email format templates odoo8.0
Avatar
Avatar
2
sep 21
6472
Email Template subject parameters Solved
email templates
Avatar
Avatar
1
feb 24
3593
Nothing rendered into email body_html (Odoo 16) Solved
email templates
Avatar
1
júl 23
4567
How to add company logo to email templates? Solved
email templates
Avatar
Avatar
Avatar
Avatar
3
dec 23
19916
Failed to render template Solved
email templates
Avatar
Avatar
1
feb 22
7131
Komunita
  • Tutoriály
  • Dokumentácia
  • Fórum
Open Source
  • Stiahnuť
  • Github
  • Runbot
  • Preklady
Služby
  • Odoo.sh hosting
  • Podpora
  • Vyššia verzia
  • Custom Developments
  • Vzdelávanie
  • Nájdite účtovníka
  • Nájdite partnera
  • Staň sa partnerom
O nás
  • Naša spoločnosť
  • Majetok značky
  • Kontaktujte nás
  • Pracovné ponuky
  • Eventy
  • Podcast
  • Blog
  • Zákazníci
  • Právne dokumenty • Súkromie
  • Bezpečnosť
الْعَرَبيّة 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 je sada podnikových aplikácií s otvoreným zdrojovým kódom, ktoré pokrývajú všetky potreby vašej spoločnosti: CRM, e-shop, účtovníctvo, skladové hospodárstvo, miesto predaja, projektový manažment atď.

Odoo prináša vysokú pridanú hodnotu v jednoduchom použití a súčasne plne integrovanými biznis aplikáciami.

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