create Sling model in htl file | Community
Skip to main content
Level 2
January 10, 2025
Solved

create Sling model in htl file

  • January 10, 2025
  • 2 replies
  • 590 views

Hi,

 

I am inside an htl html template file which uses SlingModel A  and I want to call another template, which requires a parameter which is requiring SlingModel B.

I know I can add a method to SlingModel A which instantiates SlingModell B.

May question would be if I can do instantiate SlingModel B directly in htl file ?

 

Thanks a lot for your response in advance.

 

--

Volker

Best answer by konstantyn_diachenko

Hi @vhochsteintef ,

 

Yes, in HTL (HTML Template Language), you can directly instantiate a Sling Model within your template by using HTL's data-sly-use attribute. This allows you to create an instance of Sling Model B directly in the HTL file without needing to modify Sling Model A. Here’s an example of how to achieve this:

 

Example Scenario

Sling Model A (for context)

@Model(adaptables = Resource.class) public class SlingModelA { public String getExample() { return "Hello from Model A!"; } }

Sling Model B (requires parameters)

@Model(adaptables = Resource.class) public class SlingModelB { @Inject //@RequestAttribute is an alternative @Default(values = "Default Value") private String someParameter; public String getProcessedData() { return "Processed: " + someParameter; } }

HTL Template

<!-- Using Sling Model A --> <data-sly-use.modelA="com.example.models.SlingModelA" /> <p>${modelA.example}</p> <!-- Instantiate Sling Model B with parameters --> <data-sly-use.modelB="${'com.example.models.SlingModelB' @ someParameter='Custom Value'}" /> <p>${modelB.processedData}</p>

Let me know if you need additional clarifications or further code examples.

 

Best regards,

Kostiantyn Diachenko.

2 replies

konstantyn_diachenko
Community Advisor
konstantyn_diachenkoCommunity AdvisorAccepted solution
Community Advisor
January 10, 2025

Hi @vhochsteintef ,

 

Yes, in HTL (HTML Template Language), you can directly instantiate a Sling Model within your template by using HTL's data-sly-use attribute. This allows you to create an instance of Sling Model B directly in the HTL file without needing to modify Sling Model A. Here’s an example of how to achieve this:

 

Example Scenario

Sling Model A (for context)

@Model(adaptables = Resource.class) public class SlingModelA { public String getExample() { return "Hello from Model A!"; } }

Sling Model B (requires parameters)

@Model(adaptables = Resource.class) public class SlingModelB { @Inject //@RequestAttribute is an alternative @Default(values = "Default Value") private String someParameter; public String getProcessedData() { return "Processed: " + someParameter; } }

HTL Template

<!-- Using Sling Model A --> <data-sly-use.modelA="com.example.models.SlingModelA" /> <p>${modelA.example}</p> <!-- Instantiate Sling Model B with parameters --> <data-sly-use.modelB="${'com.example.models.SlingModelB' @ someParameter='Custom Value'}" /> <p>${modelB.processedData}</p>

Let me know if you need additional clarifications or further code examples.

 

Best regards,

Kostiantyn Diachenko.

Kostiantyn Diachenko, Community Advisor, Certified Senior AEM Developer, creator of free AEM VLT Tool, maintainer of AEM Tools plugin.
AmitVishwakarma
Community Advisor
Community Advisor
January 19, 2025

You cannot directly instantiate a Sling Model in an HTL file. Instead, use a method in your Sling Model (A) to instantiate and pass the required Sling Model (B) to the HTL file. Access the model properties in the HTL using ${model.someProperty}. This maintains the separation of logic and presentation in AEM.