Expand my Community achievements bar.

SOLVED

Call parameterized method from component HTL

Avatar

Level 2

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

3 Replies

Avatar

Level 2

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.

Avatar

Correct answer by
Community Advisor

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

Avatar

Community Advisor

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