Declare sightly variables in aem dialog | Community
Skip to main content
kishore_polsani
Level 4
Jul 8, 2017 3:32 PM UTC
Solved

Declare sightly variables in aem dialog

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.
smacdonald2008
smacdonald2008Accepted solution
Level 10
Jul 10, 2017 7:09 PM UTC

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

Feike_Visser1
Employee
Jul 22, 2017 10:39 AM UTC

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

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

See HTL Expression Language

Translate
kishore_polsani
Level 4
Jul 22, 2017 5:36 AM UTC

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.

Translate
Feike_Visser1
Employee
Jul 11, 2017 7:28 AM UTC

What you could do is something like this:

${ properties[model.varname]}

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

Translate
smacdonald2008
smacdonald2008Accepted solution
Level 10
Jul 10, 2017 7:09 PM UTC

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

Translate