handlebar on email templates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    handlebar on email templates

    Hi Folks,

    It says on the documentation for email templates that it supports handlebars also it says it is experimental. I was just wondering did anyone manage to use if statement in an ameila template tried all sort of possible scenarios but failed with error message (bad token).

    documenation link https://docs.espocrm.com/user-guide/...lebars-support

    if anyone managed to use handlebars in email templates would you please care and share with us how to do it ?

    Many thanks
    Rabii
    Web Dev
  • shalmaxb
    Senior Member
    • Mar 2015
    • 1606

    #2
    Hi,
    here is a working example:

    It is about renting artwork (up to four works of art. It is a reminder, that the renting is ending soon. The if statement only indicates, if there are one, two, three or four artworks are involved. In case it is only one, the other three lines would not be displayed and so on. It is in German, but I guess it is understandable.
    I hope, it helps. If not, feel free to post your code here, perhaps we can help.

    Code:
    <p>Liebe/r&nbsp; {User.salutationName}&nbsp;{User.lastName},</p>
    <p>der Mietvertrag&nbsp;{Vermietung.name}, Nr.&nbsp;{Vermietung.mietvertragNr} wird am&nbsp;{Vermietung.mieteEnde} ablaufen.</p>
    <p>Bitte veranlassen Sie rechtzeitig die Rücklieferung folgenden Werkes/folgender Werke:</p>
    <p>{{#if werkeVermietung1Name}}{Vermietung.werkeVermietung1.kuenstler}<b>&nbsp;{Vermietung.werkeVermietung1Name}</b></p>{{/if}}
    <p>{{#if werkeVermietung2Name}}{Vermietung.werkeVermietung2.kuenstler}<b>&nbsp;{Vermietung.werkeVermietung2Name}</b></p>{{/if}}
    <p>{{#if werkeVermietung3Name}}{Vermietung.werkeVermietung3.kuenstler}<b>&nbsp;{Vermietung.werkeVermietung3Name}</b></p>{{/if}}
    <p>{{#if werkeVermietung4Name}}{Vermietung.werkeVermietung4.kuenstler}<b>&nbsp;{Vermietung.werkeVermietung4Name}</b></p>{{/if}}
    
    <p>oder schließen Sie einen Verlängerungsvertrag ab.<br></p><p>Diese Email wird vom System Werkverzeichnis zwei Wochen vor Ablauf der Mietperiode automatisch versendet.</p><p>Mit freundlichen Grüßen</p><p><br></p><p>xxxx, kunstservice</p>
    To explain:

    First line: Salutation and name of client
    Second Line: Data of the contract
    Third Line: subject of the text
    Line 4-7: Data of the rented artwork number one to four
    Line 8 to end: explaining text etc.

    I always start in the emal editor, then copy the code to a normal text editor like notepad++ to clean and format the code. This makes the code much more readable and you can delete all the unnecessary code parts, that the editor puts in. This is because the editor used in espoCRM is summernote editor, that messes up the code heavily.
    If you want to have any CSS, this editor will strip it out, unless you code it inline. That way it keeps nearly all of css. To inline code, you may write it already that way or you write a css file, that you may convert together with the html to inline code, e.g.: https://templates.mailchimp.com/resources/inline-css/.

    Comment


    • rabii
      rabii commented
      Editing a comment
      Hi @shalmaxb 

      Thanks for sharing this, much appreciated. i have just tried it but didn't work, i just tried to email an account with a template that checks if we have an account website, account name, account industry then they should be printed otherwise provide an alternative text to explain that we could't find a website for this account etc please see below my code: 

      Code:
      <p>{{#if Account.website}}{Account.website}<b>&nbsp;no website found</b></p>
      {{/if}}
      <p>{{#if Account.industry}}{Account.industry}<b>&nbsp;No industry given</b></p>
      {{/if}}
      <p>{{#if Account.name}}{Account.name}<b>&nbsp;name is not provided</b></p>
      {{/if}}
      Did i do something wrong, when email is received it is blank and has no content at all. 

      Thanks 
      ​​​​​​​Rabii
  • shalmaxb
    Senior Member
    • Mar 2015
    • 1606

    #3
    It is a bit difficult to detect, where the mistake lies. I will try to explain, what conditions have to be met in my template:

    1. In my entity there are four obejcts (artwork to rent) possible. Each one is identified in the template by number (1,2,3,4).
    2. If artwork 1 has data it will be output with artist`s name, title of artwork
    3. If artwork 2 also has data, the sam

    and so on.

    If you want to output the text "no website found", when the field Account.website is empty, the code should lokok like this:


    <p>{{#if Account.website}}{{Account.website}}<b>&nbsp;no website found</b></p> {{/if}}

    Pay attention to the second Account.website, that also has double curly brackets. I am not 100% sure, because I cannot test here. If you cannot get it to work via handlebars code, you could create another field, which you fill by formula and hand that over to your template. That way it is easier and more confident.

    Comment

    • rabii
      Active Community Member
      • Jun 2016
      • 1250

      #4
      Hi thanks for your reply. If in understand your template has access to main entity and its related entities.

      i have tried all possible scenarios :

      1 - <p>{{#if Account.website}}{{Account.website}}<b>&nbsp;no website found</b></p> {{/if}} (didn't work)
      2 - {{#if Account.website}} {{Account.website}} {{/if}} (didn't work)
      3 - {{#if Account.website}} {Account.website} {{/if}} (didn't work)

      I will just keep trying and see if this would work. Just to make things clear we are talking about email template not pdf template right ? i know pdf template works fine but i am trying to use the handlebars on the email template (html email templates).

      Many thanks for your time and support much appreciated.

      Cheers
      Rabii
      Web Dev

      Comment


      • shalmaxb
        shalmaxb commented
        Editing a comment
        Yes, my code is from an email template and works as desired.
        The first code-part is from main entity, the second code-part is from linked entity and the third code-part is from another linked entity. There are three different fields involved.
        Because of that I mentioned to create a field, where you place your logic and use that new field for the template output.
    Working...