Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Is there a way to access a convenience variable set in a template?

Avatar

Level 3

Hello!

 

I have a template where I set a convenience variable using data-sly-set:

 

<sly data-sly-template.myTemplate="${@ parameter}">
<sly data-sly-use.myModel="${'com.xxx.xxx.xxx.core.models.MyModel' @ parameter=parameter}" data-sly-set.myVariable="${myModel.method}"/>
</sly>

 

 

Is there a way I can access this variable in sightly code that calls this template?

 

The reason why I'm trying to figure this out is because we have multiple repositories, and we want to avoid having to include a Java dependency in every repo. We'd rather have a soft dependency, hence the template.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @IshaJa 

 

I have found this https://gist.github.com/dlh3/248aa56ba7301f65e29f435c7cb03de0 which can help you in a way.

Basically, you are setting some value on calling template and then after calling the template, you need to access the set variable in main HTL.

 

Please go through https://gist.github.com/dlh3/248aa56ba7301f65e29f435c7cb03de0 and on high level the approach is:

1. You can call template and whithin template, you use RequestAttributesHelper.java to save the values with key and value form in Request Attributes.

2. with in main HTL after calling template, you can now again use RequestAttributesHelper.java using data-sly-use to get the value using its key.

3. Value will be accessible as it is now part of the SlingHTtpServletRequest and hence is accessible everywhere once set.

 

Hope it helps!

Thanks,

nupur

View solution in original post

11 Replies

Avatar

Community Advisor

You can access the variable set in aem sightly as explained on https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-declare-a-variable-... but not sure if it will works in your case like sharing it among different repositories

Avatar

Level 3

Hi @DPrakashRaj, my question was how to access a variable that is set in a template from Sightly code that references that template. I've tried with both data-sly-test and data-sly-set, and while I can access it within the template, I cannot seem to access it in code that uses that template. 

Avatar

Level 3

@DPrakashRaj yes I have, I showed that in my post. But the purpose of my template is not to retrieve an HTML object. it's to get a string, specifically a URL.

I can't simply data-sly-call it over and over again because that wouldn't be efficient. I want to be able to access the value that the template generates and set it to a variable.

Avatar

Community Advisor

Hi @IshaJa ,

In Adobe Experience Manager (AEM), Sightly (HTL) is designed to keep the markup and logic cleanly separated. Variables set within a data-sly-template scope are generally intended to be used only within that template. However, if you need to expose a variable from within a template to the outer context where the template is called, there are a few approaches you can consider.

Approach 1: Using Data-Sly-Resource

One common approach is to leverage data-sly-resource to include a component that can use the model and expose its properties. This way, you can encapsulate the logic inside the component and reuse it without adding Java dependencies to multiple repositories.

Approach 2: Passing Values Back via Template Parameters

Another approach involves passing values back from the template by using additional parameters. This way, you can define a parameter that the caller can use to receive the computed value.

Example of Approach 2

Here's a detailed example:

Define the Template

In your template file (e.g., myTemplate.html), you set up the template to take an additional parameter to pass the computed value back.

 

<sly data-sly-template.myTemplate="${@ parameter, returnVariable=''}">
    <sly data-sly-use.myModel="${'com.xxx.xxx.xxx.core.models.MyModel' @ parameter=parameter}" />
    <sly data-sly-set.var="${myModel.method}" />
    <sly>${returnVariable = var}</sly>
</sly>

 

Call the Template and Access the Variable

In the calling script (e.g., caller.html), you can call the template and use the returned value.

 

<sly data-sly-call="${myTemplate @ parameter='someValue', returnVariable=use.returnVariable}" />
<p>Computed Value: ${returnVariable}</p>

 

Notes:

  • data-sly-template allows you to define reusable code blocks with parameters.
  • data-sly-call is used to invoke these templates.
  • By passing an additional parameter (returnVariable), you can capture the computed value from within the template and then use it in the calling context.

This method allows you to create a soft dependency where the logic resides within the template, and the calling context can access the results without directly dealing with the underlying Java model. It effectively separates concerns and avoids the need to include Java dependencies in multiple repositories.




 

Avatar

Community Advisor

Hi @IshaJa 
Yes, you can use that, this should work.

If does not then try with data-sly-test

 

<sly data-sly-use.myModel="${'com.xxx.xxx.xxx.core.models.MyModel' @ parameter=parameter}" data-sly-test.myVariable="${myModel.method}"/>


Arun Patidar

Avatar

Level 3

Hi @arunpatidar, I did try with both data-sly-test and data-sly-set, but it wasn't able to detect the variable. Got any other suggestions?

Also just to clarify, the variable is set in the template, and I need to be able to access that from files that reference that template. 

Avatar

Correct answer by
Community Advisor

Hi @IshaJa 

 

I have found this https://gist.github.com/dlh3/248aa56ba7301f65e29f435c7cb03de0 which can help you in a way.

Basically, you are setting some value on calling template and then after calling the template, you need to access the set variable in main HTL.

 

Please go through https://gist.github.com/dlh3/248aa56ba7301f65e29f435c7cb03de0 and on high level the approach is:

1. You can call template and whithin template, you use RequestAttributesHelper.java to save the values with key and value form in Request Attributes.

2. with in main HTL after calling template, you can now again use RequestAttributesHelper.java using data-sly-use to get the value using its key.

3. Value will be accessible as it is now part of the SlingHTtpServletRequest and hence is accessible everywhere once set.

 

Hope it helps!

Thanks,

nupur

Avatar

Community Advisor

Yeah you would just keep passing down variable through templates.

 

<!-- card.html -->
<sly data-sly-set.variable="test"></sly>
<div data-sly-use.template="card.template.html">
    <div data-sly-call="${template.template @ title=variable}"></div>
</div>

<!-- card.template.html -->
<template data-sly-template.template="${@ title}">
    <h1>${title}</h1>
</template>

 


hope this helps.

Avatar

Administrator

@IshaJa Did you find the suggestion helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni