How to hide html tag in Email template? | Community
Skip to main content
Level 3
Aug 11, 2019 7:03 AM UTC
Solved

How to hide html tag in Email template?

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 Patidar​ antoniom54959291​ Jörg Hoh​ smacdonald2008​ kautuksahni​

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Himanshu_Singhal
Himanshu_SinghalAccepted solution
New Participant
Aug 13, 2019 5:51 AM UTC

  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.

5 replies

Himanshu_Singhal
Himanshu_SinghalAccepted solution
New Participant
Aug 13, 2019 5:51 AM UTC

  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.

Translate
tahir1601Author
Level 3
Aug 13, 2019 3:56 AM UTC

can you provide sample code of what you did

Translate
Gaurav-Behl
Level 10
Aug 12, 2019 12:17 PM UTC

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.

Translate
arunpatidar
New Participant
Aug 12, 2019 9:51 AM UTC

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.

Translate
Arun Patidar
Himanshu_Singhal
New Participant
Aug 11, 2019 6:52 PM UTC

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

Translate