Expand my Community achievements bar.

SOLVED

Handling JSP expressions in HTL/Sling Model

Avatar

Level 5

Hello Community - We have a JSP based template which needs to be converted into HTL. Currently we have planned to move all the business logic in Sling Model. Can you advise the best approach to handle the below in HTL.

 

A) <c:set var="runMode" value="uat" scope="request"/> - We can use <div data-sly-test.runMode="uat"> to assign a variable name, which can later be used like <div>${runMode}</div>. If the assignment is for few variables, we can use this approach, but this is not a best approach. Is there any better solution to handle this ?

B) request.setAttribute("isProductActive", isProductActive); - In the current implementation, we are setting the values in request. How can we handle this? If this has to be done in Sling model. How do we use this in HTL.
C) pageContext.setAttribute("fetchData",fetchData); - This is also required to handle in HTL.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @v1101,

 

Here is what you can do:

 

A. If the run mode that you are setting is STATIC, you can create a new string variable in the sling model and access it in HTL.

Java:

private String runMode;

HTL:

<div data-sly-test.runMode="${object.runMode}">

Also, you can let author select the value via dialog.

 

You can also get it via slingSettingsService object:

slingSettingsService.getRunModes();

 

B. In sling models request attribute can be set using the request object.

Set the model adaptable to SlingHttpServletRequest

@Model(adaptables = SlingHttpServletRequest.class)

 

Inject the request:

@Inject
SlingHttpServletRequest request;

 

Using this request attribute, you can set the value in any attribute you need and send it to HTL.

 

C.  The pageContext object can be used to set, get or remove the attribute from the page, request, session, application.

 

You can set the context in sightly like this :

${properties.nodename @ context='html'}

 

Not sure why you need pageContext here, but here is the list of available global objects in HTL: https://experienceleague.adobe.com/docs/experience-manager-htl/using/htl/global-objects.html?lang=en...

 

Thanks,

Kiran Vedantam.

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @v1101,

 

Here is what you can do:

 

A. If the run mode that you are setting is STATIC, you can create a new string variable in the sling model and access it in HTL.

Java:

private String runMode;

HTL:

<div data-sly-test.runMode="${object.runMode}">

Also, you can let author select the value via dialog.

 

You can also get it via slingSettingsService object:

slingSettingsService.getRunModes();

 

B. In sling models request attribute can be set using the request object.

Set the model adaptable to SlingHttpServletRequest

@Model(adaptables = SlingHttpServletRequest.class)

 

Inject the request:

@Inject
SlingHttpServletRequest request;

 

Using this request attribute, you can set the value in any attribute you need and send it to HTL.

 

C.  The pageContext object can be used to set, get or remove the attribute from the page, request, session, application.

 

You can set the context in sightly like this :

${properties.nodename @ context='html'}

 

Not sure why you need pageContext here, but here is the list of available global objects in HTL: https://experienceleague.adobe.com/docs/experience-manager-htl/using/htl/global-objects.html?lang=en...

 

Thanks,

Kiran Vedantam.