Passing parameters to Sling Model methods | Community
Skip to main content
November 20, 2024
Solved

Passing parameters to Sling Model methods

  • November 20, 2024
  • 2 replies
  • 1330 views

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'}">
 
Best answer by Tethich

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.

2 replies

lukasz-m
Community Advisor
Community Advisor
November 20, 2024

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:

 

Tethich
Community Advisor
TethichCommunity AdvisorAccepted solution
Community Advisor
November 20, 2024

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.