Recommended way of printing simple dialog fields in AEM HTL. | Adobe Higher Education
Skip to main content
Nikhil-Kumar
Community Advisor
Community Advisor
August 31, 2022
Respondido

Recommended way of printing simple dialog fields in AEM HTL.

  • August 31, 2022
  • 4 respostas
  • 1340 Visualizações

 

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.

Este tópico foi fechado para respostas.
Melhor resposta por arunpatidar

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");

4 Respostas

August 31, 2022

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 @14766979 @1889392 at class level and you can access them in sightly through your model. 

Hope this helps.

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorResposta
Community Advisor
August 31, 2022

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
VeenaVikraman
Community Advisor
Community Advisor
September 1, 2022

@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 ✌

Adobe Employee
September 1, 2022

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