Expand my Community achievements bar.

SOLVED

Declare sightly variables in aem dialog

Avatar

Level 4

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.

1 Accepted Solution

Avatar

Correct answer by
Level 10

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

View solution in original post

4 Replies

Avatar

Correct answer by
Level 10

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

Avatar

Employee

What you could do is something like this:

${ properties[model.varname]}

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

Avatar

Level 4

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.

Avatar

Employee

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

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

See HTL Expression Language