Is there a way to access a convenience variable set in a template? | Community
Skip to main content
Level 2
June 12, 2024
Solved

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

  • June 12, 2024
  • 6 replies
  • 1755 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Nupur_Jain

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

6 replies

DPrakashRaj
Community Advisor
Community Advisor
June 12, 2024

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-in-sightly/m-p/256691 but not sure if it will works in your case like sharing it among different repositories

IshaJaAuthor
Level 2
June 13, 2024

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. 

DPrakashRaj
Community Advisor
Community Advisor
June 13, 2024
HrishikeshKagne
Community Advisor
Community Advisor
June 13, 2024

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.




 

Hrishikesh Kagane
Level 6
June 13, 2024

Thanks ChatGPT

arunpatidar
Community Advisor
Community Advisor
June 13, 2024

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
IshaJaAuthor
Level 2
June 13, 2024

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. 

Nupur_Jain
Adobe Employee
Nupur_JainAdobe EmployeeAccepted solution
Adobe Employee
June 13, 2024

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

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
June 14, 2024

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.

kautuk_sahni
Community Manager
Community Manager
June 19, 2024

@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