Expand my Community achievements bar.

SOLVED

create Sling model in htl file

Avatar

Level 3

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

1 Accepted Solution

Avatar

Correct answer by
Level 9

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.

View solution in original post

2 Replies

Avatar

Correct answer by
Level 9

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.

Avatar

Level 7

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.