AEM Tip: Passing String Arguments from HTL to the Back End

Share article

Tech Tips

Read our other AEM tips

HTL (HTML Template Language) is what Adobe recommends for rendering HTML markup in AEM (Adobe Experience Manager), instead of using JSP (JavaServer Pages). Although HTL has many advantages, it leaves out several things used in JSP, so we need to consider workarounds for HTL in these situations.

For instance, consider a situation in which you need to pass a string argument from HTL to the back end. Let’s say, a user name is returned by a service, and we need to render a data-attribute containing a token associated with that user name.

We can leverage the fact that HTL supports expressions like this:

Here myObject can be an instance of the java.util.Map. So, we can create a helper class with a field that pretends to be a Map:

The helper class uses a factory to create a map proxy:

The factory, in turn, uses the invocation handler below for the map proxy:

Now that we’ve set up all that, we can use the helper class in HTL to render a username-based token:

Author: Denis Glushkov

Do you love open source projects?

Join our team!

Do you love open source projects?

Join our team!