Expand my Community achievements bar.

SOLVED

Passing parameters to Sling Model methods

Avatar

Level 2

I have method with arguments in Sling model. while calling that method from HTL facing below issue in Log for passing parameters. please advise

 

SlingRequestProcessorImpl service: Uncaught SlingException
org.apache.sling.scripting.sightly.SightlyException: org.apache.sling.api.SlingException: Cannot get DefaultSlingScript:  org.apache.sling.scripting.sightly.SightlyException: org.apache.sling.api.scripting.ScriptEvaluationException: ${cfmGenModel.createContentFragment @ '//content//dam//my-cfm', 'popup-fragment', '//conf//consumer-bau//settings//dam//cfm//models//bau-model-popup'}: mismatched input ''//content//dam//my-cfm'' expecting ID in /apps/mysite/components/excelCFM/excelCFM.html at line number 9 at column number 59
 
Sling Model Method:
public void createContentFragment(String parentPath, String fragmentName, String modelPath) {}
 HTL Method Call:
<div data-sly-use.cfmGenModel="com.myproject.core.models.CFMImporter">
<div data-sly-call="${cfmGenModel.createContentFragment @ '/content/dam/my-cfm', 'popup-fragment', '/conf/consumer-bau/settings/dam/cfm/models/bau-model-popup'}">
 
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 8

Ho @Mohamed-Valith-J 

It is not possible to achieve what you need in the way you want. There is still an open request for this, but me personally I don't think it will be implemented: 

https://github.com/adobe/htl-spec/issues/86

 

The way to do it is how @lukasz-m suggested. That is the tipical approach.

 

The best approach would be to keep view sepatate from the controller, and not have hard-coded values in HTL. I advice to move the logic only in the model and have control over it via component properties. This means to have dialog fields to configure the component with stuff that will instruct your model to execute its logic for a given path, a given CF type and a given config.

View solution in original post

2 Replies

Avatar

Community Advisor

Hi @Mohamed-Valith-J,

This is not supported. It is not possible to use parametrized method from Sling Model on HTL level.

What can be done, you can pass values/params during initialization phase via Request Attribute. So your class could look like below:

@Model(adaptables = {SlingHttpServletRequest.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class CFMImporter {
    
    @RequestAttribute
    private String parentPath;

    @RequestAttribute
    private String fragmentName;

    @RequestAttribute
    private String modelPath;

    public void createContentFragment() {}
}

HTL:

<div data-sly-use.cfmGenModel="${'com.myproject.core.models.CFMImporter' @ parentPath='/content/dam/my-cfm', fragmentName='popup-fragment', modelPath='/conf/consumer-bau/settings/dam/cfm/models/bau-model-popup'}">
${cfmGenModel.createContentFragment}
</div>

Please also check below documentation:

 

Avatar

Correct answer by
Level 8

Ho @Mohamed-Valith-J 

It is not possible to achieve what you need in the way you want. There is still an open request for this, but me personally I don't think it will be implemented: 

https://github.com/adobe/htl-spec/issues/86

 

The way to do it is how @lukasz-m suggested. That is the tipical approach.

 

The best approach would be to keep view sepatate from the controller, and not have hard-coded values in HTL. I advice to move the logic only in the model and have control over it via component properties. This means to have dialog fields to configure the component with stuff that will instruct your model to execute its logic for a given path, a given CF type and a given config.