April 26, 2020
Estimated Post Reading Time ~

Generic way to author text in AEM dialog

I am trying to author text in dialog, the text contains dynamic values like name, etc. I tried declaring sightly variable names in AEM dialog to fetch the data dynamically but it didn't work.



I tried the below approaches.

Approach 1:


/*
  * Using String format
  */
  String authoredText= "Hi %s, welcome to %s";
  authoredText = authoredText.format(authoredText,"Kishore","AEM Quickstart");
  log.info("---Using String format----\n"+authoredText);

Approach 2:

/*
  * Using StrSubstitutor - org.apache.commons.lang.text.StrSubstitutor
  */
  Map valuesMap = new HashMap();
  valuesMap.put("name", "Kishore");
  valuesMap.put("blog", "AEM Quickstart");
  String templateString = "Hi ${name}, welcome to ${blog}.";
  StrSubstitutor sub = new StrSubstitutor(valuesMap);
  String resolvedString = sub.replace(templateString);
  log.info("---Using StrSubstitutor----\n"+resolvedString);

Approach 3:

/**
   * Using String replace
   */
  String authoredText= "Hi ${name}, welcome to ${blog}";
  authoredText = authoredText.replace("${name}","Kishore");
  authoredText = authoredText.replace("${blog}","AEM Quickstart");
  log.info("---Using String replace----\n"+authoredText);

From the above approaches, we can author the text in any of the formats and parse it in java and we can get the text in HTL.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.