BREAKING NEWS: Exadel named a Leader in this Forrester Agile Software Development Service Providers report... Learn more!  

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

 
 
 

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:

NEW! Want to test new technologies like blockchain, AI, mobile, chatbot, and machine learning models? Learn about our Innovation Lab.