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.