Expand my Community achievements bar.

SOLVED

How to hide html tag in Email template?

Avatar

Level 4

Hi,

I have an email template in etc/notifications folder which is of type html. I am trying to hide an element based on variable i fetch from email service params(of email service).

Below is my template code

From: ${senderEmailAddress}

Subject:${subject}

<hr>

<b>Vous êtes ?</b> ${role}

<b>Vous êtes ?</b> ${subrole}

<b>Choisissez votre profession</b> ${profession}

<b>Nom de l'entreprise</b> ${enterpriseName}

<b>Adresse</b> ${address}

<b>Nom</b> ${Name}

<b>Prénom</b> ${firstName}

<b>Téléphone</b> ${telephone}

<b>Email</b> ${email}

<b>Code postal</b> ${postalCode}

<b>Ville</b> ${ville}

<b>Pays</b> ${country}

<b>Objet de la demande</b> ${objectOfDemand}

<b>Type de produits</b> ${typeOfProducts}

<b>Veuillez saisir la référence du ou des produits concernés</b> ${reference}

<b>Saisissez votre demande</b> ${info}

<b>Produit(s) sous garantie</b> ${warranty}

<b>En cochant cette case, vous acceptez de recevoir des informations commerciales de la part de Jacob Delafon pour des produits ou des services analogues à ceux que vous avez consultés.</b> ${subscription}

<hr>

Now I want to hide '<b>' tag if ${something} is null or empty. Please suggest Arun Patidarantoniom54959291Jörg Hohsmacdonald2008kautuksahni

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

  private String getSubstitutedString(Map<String, String> valueMap, String beforeParsing) {

    VelocityEngine ve = new VelocityEngine();

    ve.init();

    VelocityContext context = new VelocityContext();

    StringWriter writer = new StringWriter();

    for (Map.Entry<String, String> entry : valueMap.entrySet()) {

      String value = xssApi.filterHTML(entry.getValue());

      value = Jsoup.parse(value).text();

      context.put(entry.getKey(), value);

    }

    ve.evaluate(context, writer, "VelocityParser", beforeParsing);

    return writer.toString();

    }

Here, the argument beforeParsing is the velocity syntax template String that contains condition based variables and its values will be replaced by actual values which are in Map.

View solution in original post

5 Replies

Avatar

Community Advisor

You can use the Apache Velocity templating engine to add condition based Text in Email.

We had the same requirement once and utilised Apache Velocity Templating engine and it's quite nice.

https://www.baeldung.com/apache-velocity

Avatar

Community Advisor

Hi,

I think its not possible with OOTB AEM templates, because you can't run javascript in mail(not supported in many mail client).

But you can set both Label and value in key and return "" if something is null.



Arun Patidar

Avatar

Level 10

As mentioned by Himanshu/Arun, this is not supported OOB but you can still use any template engine (client/server side) like  Velocity/Handlebars/Mustache etc. and get the results. You may also try for a PostProcessor if your use case requires it.

Avatar

Level 4

can you provide sample code of what you did

Avatar

Correct answer by
Community Advisor

  private String getSubstitutedString(Map<String, String> valueMap, String beforeParsing) {

    VelocityEngine ve = new VelocityEngine();

    ve.init();

    VelocityContext context = new VelocityContext();

    StringWriter writer = new StringWriter();

    for (Map.Entry<String, String> entry : valueMap.entrySet()) {

      String value = xssApi.filterHTML(entry.getValue());

      value = Jsoup.parse(value).text();

      context.put(entry.getKey(), value);

    }

    ve.evaluate(context, writer, "VelocityParser", beforeParsing);

    return writer.toString();

    }

Here, the argument beforeParsing is the velocity syntax template String that contains condition based variables and its values will be replaced by actual values which are in Map.