Declare sightly variables in aem dialog | Community
Skip to main content
kishore_polsani
Level 4
July 8, 2017
Solved

Declare sightly variables in aem dialog

  • July 8, 2017
  • 4 replies
  • 4870 views

Hi,

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 have authored a textarea widget (./desc) in aem dialog as "Hi ${model.name}, welcome to adobe forum." Where model is the sightly class object, and name is the variable declared in sightly wcmusepojo class.

In front I tried to get the text by adding ${properties.desc @context='html'}.

Can u help me how we can author the text dynamically.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

You cannot use variables in the dialog fields. Dialog fields have names (for example ./first) and you can fetch the names using code. If you are using WCMUsePojo - you can read the dialog fields and store them to a data member that belongs to a class. Then you can write out the value in the HTL front end. See:

Scott's Digital Community: Creating an AEM HTML Template Language component that uses the WCMUsePojo class

4 replies

smacdonald2008
smacdonald2008Accepted solution
Level 10
July 10, 2017

You cannot use variables in the dialog fields. Dialog fields have names (for example ./first) and you can fetch the names using code. If you are using WCMUsePojo - you can read the dialog fields and store them to a data member that belongs to a class. Then you can write out the value in the HTL front end. See:

Scott's Digital Community: Creating an AEM HTML Template Language component that uses the WCMUsePojo class

Feike_Visser1
Adobe Employee
Adobe Employee
July 11, 2017

What you could do is something like this:

${ properties[model.varname]}

When model.varname gets a property-name, then that should work.

kishore_polsani
Level 4
July 22, 2017

Hi Feike,

I tried your suggestion 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");

  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 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.

Feike_Visser1
Adobe Employee
Adobe Employee
July 22, 2017

No need for Java here, you can do this in HTL:

${'Page {0} of {1}' @ format=[current, total]}

See HTL Expression Language