Saturday, 22 July 2017

Generic way to author text in AEM dialog

I am trying to author text in dialog, 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 below approaches..

Approach 1:
  * Using String format
  String authoredText= "Hi %s, welcome to %s";
  authoredText = authoredText.format(authoredText,"Kishore","AEM Quickstart");"---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);"---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");"---Using String replace----\n"+authoredText);

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

No comments :

Post a Comment