Call parameterized method from component HTL | Community
Skip to main content
Level 2
June 21, 2022
Solved

Call parameterized method from component HTL

  • June 21, 2022
  • 3 replies
  • 1880 views

We have a requirement where we have created content fragment model having one of the fields as location title. Now we are getting list of content fragment objects (officeLocationCfms) in component HTL Code. We need to display content fragment location title value in component's UI.

 

<div data-sly-use.contactcards="com.XXXXXX.core.components.models.ContactCards"
   class="cmp-contactcards" id="${contactcards.id}">
   <div class="cmp-contactcard" data-sly-repeat.officeLocationCfm="${contactcards.getOfficeLocationCfms}">
      <div class="cmp-contactcard__elements">
         <div data-sly-use.officeLocationTitle="${officeLocationCfm.getElement @ text='title'}" class="cmp-contactcard__element cmp-contactcard__title">
            ${officeLocationTitle.getContent}
         </div>
      </div>
   </div>
</div>

 

Method: com.adobe.cq.dam.cfm.ContentFragment.getElement(String var1)

 

But its not working.

 

I wanted to know how to call parameterized methods from HTL.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by ShaileshBassi

Hi @s_k_agarwal  ,

Try passing multiple parameters as mentioned below, for sample i have hardcoded parameter as string values here & replace these hardcoded value by dynamic values. please make sure datatype of dynamic value getting passed in sightly is same as datatype defined for @Inject variable in Sling Model class. 

 

Sightly:

<sly data-sly-test.file="/some/path/to/file" data-sly-test.image="image">
<sly data-sly-use.helloWorldModel="${'com.project.HelloWorldModel' @fileReference=file, imgParam=image}"/>
</sly>

 

Sling Model Class:

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class HelloWorldModel {

@Inject
private String fileReference;

@Inject
private String imgParam;

 

Hope this helps!

Thanks

3 replies

Level 2
June 21, 2022

Hi @s_k_agarwal 

I don't think it is possible to call parameterized method. We can only call getter non-parametrized methods as per my knowledge.

However I would recommend instead of calling your method in HTL, you should write business logic in your existing backend sling model class com.XXXXXX.core.components.models.ContactCards  by calling 

getElement(String var1and send an updated list from the sling model and use that list in HTL.

ShaileshBassi
Community Advisor
ShaileshBassiCommunity AdvisorAccepted solution
Community Advisor
June 21, 2022

Hi @s_k_agarwal  ,

Try passing multiple parameters as mentioned below, for sample i have hardcoded parameter as string values here & replace these hardcoded value by dynamic values. please make sure datatype of dynamic value getting passed in sightly is same as datatype defined for @Inject variable in Sling Model class. 

 

Sightly:

<sly data-sly-test.file="/some/path/to/file" data-sly-test.image="image">
<sly data-sly-use.helloWorldModel="${'com.project.HelloWorldModel' @fileReference=file, imgParam=image}"/>
</sly>

 

Sling Model Class:

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class HelloWorldModel {

@Inject
private String fileReference;

@Inject
private String imgParam;

 

Hope this helps!

Thanks

arunpatidar
Community Advisor
Community Advisor
June 21, 2022

Hi,

This is only possible if the parameter is injected to the model. I would suggest to check to retuen the title in the same model as map, because map support param 

Example

List

[{"getOfficeLocationCfm":"/content/dam/myprj/cmf/.....","cmfTitle":"Hello"}]

HTL

<div data-sly-use.officeLocationTitle="${officeLocationCfm['cmfTitle']}" class="cmp-contactcard__element cmp-contactcard__title">

 

 

Otherway is to pass the Cf path to HTL Model and get theTitle ,

e.g.

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/ParamModel.java 

Arun Patidar