Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Recommended way of printing simple dialog fields in AEM HTL.

Avatar

Community Advisor

 

What is the recommended way of printing the simple dialog fields values in HTL ?  (No business logic update needed for those dialog fields)
1. Directly printing it using ${properties.value} OR
2. Injecting to Sling Model and using Getter Method of each field to print the value in HTL.

 

In Second case of numbers of dialog fields increases then number of getter methods and Inject Statements increases. 

 

So just wondering which is the correct and recommended way of rendering simple dialog field values in HTL.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

This will also work  ${properties.value}

 

With ${properties.title}, when the HTL are converted into java and it resolve as like below before executing

Object _dynamic_properties = bindings.get("properties");
renderContext.getObjectModel().toString(renderContext.call("xss", renderContext.getObjectModel().resolveProperty(_dynamic_properties, "title")

 

with sling model Model

_global_myobj = renderContext.call("use", com.myapp.core.models.MyModel.class.getName(), obj());
_global_title = renderContext.getObjectModel().resolveProperty(_global_myobj, "title");


Arun Patidar

View solution in original post

4 Replies

Avatar

Level 1

Hi Nikhil, it depends on the use case, but I prefer Sling Model approach over directly using through properties, as with Sling models, you always have the flexibility to expose your model json with anyone. Moreover, you can use Lombok plugin to avoid writing getters and setters by simply using @getter @Setter at class level and you can access them in sightly through your model. 

Hope this helps.

Avatar

Correct answer by
Community Advisor

Hi,

This will also work  ${properties.value}

 

With ${properties.title}, when the HTL are converted into java and it resolve as like below before executing

Object _dynamic_properties = bindings.get("properties");
renderContext.getObjectModel().toString(renderContext.call("xss", renderContext.getObjectModel().resolveProperty(_dynamic_properties, "title")

 

with sling model Model

_global_myobj = renderContext.call("use", com.myapp.core.models.MyModel.class.getName(), obj());
_global_title = renderContext.getObjectModel().resolveProperty(_global_myobj, "title");


Arun Patidar

Avatar

Community Advisor

@Nikhil-Kumar Personally , my thoughts are like below. 

 

  1. If the component is very simple and don't need model , I prefer to use properties. 
  2. Use Sling Model only if backend logic / backend code is required. And as @ankyadobe  has mentioned you can always use Lombok to reduce the code.

 

I have not seen any recommendation from Adobe which says we should be using Sling Models always. that said, I have been in project where they had made it a mandatory practice to write Sling Model for every component they create. Even a simple text component. 

 

Also , if you go by core components approach/proxy components practice ; a sling model is required. 

From what @arunpatidar has mentioned, it is clear that there is not much difference in how the value is compiled to render in the HTL. I would also love to see more expert opinions as I also had this same question always. 


Veena ✌

Avatar

Employee Advisor

Hi @Nikhil-Kumar ,

 

As per my knowledge, According to Adobe best practice you should write Sling Models always, even for simple components with very less number of properties.

 

Hope This helps!!

 

Thanks