Showing posts with label Sightly tips. Show all posts
Showing posts with label Sightly tips. Show all posts

Thursday 14 February 2019

Sling Models vs WCMUSEPOJO

AEM component back-end logic was shifted over years from JSP’s to WCMUse class to WCMUsePOJOs and then to Sling Models.

AEM 6.1 or 6.2 were supporting WCMUsePojo class; AEM 6.2 and 6.3 started to support the Sling Models approach.

Both WCMUsePOJOs and Sling Models are used with HTL using <data-sly-use> block.

Before we jump into the difference , let us understand the concepts.

What is a POJO(Plain Old Java Objects)?

POJO's are simple Java classes which doesnt depend on other libraries, interfaces or annotations. This increases the chance that this can be reused in multiple project types. POJO's provides Getter and Setter methods, which allow you to change the underlying data type without breaking the public interface of your class which makes it more robust and resilient to changes.

What if there are no getters and setters?

Say we havent created setter and getter methods, then any one can directly call the variable and it surely will affect to the code, which may lead to security issues. Here POJO class are forcing other coder to call on the methods rather than directly calling the Instance variables.

What is Sling Models?

Sling Models are annotation driven POJOs. They allows to map resource properties, assign default values, inject OSGI services and much more.

Sling Models are pure POJOs that gives wonderful separation between logic and presentation which also extensible with custom injectors and annotations. Sling Models let you map Java objects to Sling resources. 

Use API Vs Sling Model

Use API

HTL(Formerly known as Sightly) uses  two ways of implementing support for business logic objects:
1) Java Use-API, through POJOs,
2) JavaScript Use-API,

Regular POJOs that extend WCMUsePojo implement the Use interface and are initialized with the scripting bindings, providing convenience methods for accessing commonly used objects like request, resource, properties, page etc.

HTL implementation from Sling provides the basic POJO support through the org.apache.sling.scripting.sightly.pojo.Use interface and the JavaUseProvider, whereas the use function is implemented by the org.apache.sling.scripting.sightly.js.provider bundle.

The Sling implementation provides a few extensions to the Use-API.


Sling Model API:

Sling Models are more flexible which can also be used outside HTL, thus makes the business-logic more reusable. They are managed by Sling and can be injected references to other objects using annotations and reflection.

Click on image to see it big


Conversion from WCMUsePojo to SlingModel
Just by adapting the above said methods (Remove 'Extends WCMUse', Add sling model annotation on top of the class, then add inject methods to invoke the references.) a developer can easily convert WCMUsePojo to SlingModel.

Conclusion:

For the versions AEM 6.3, AEM 6.4 and AEM Core WCM Components, Adobe recommends using Sling Models as the best practice.
 
More Details Here

Picking-the-best-use-provider-for-a-project

Video of the comparison 
 

Read More:

Sling Model Vs Sling Model Exporter


Friday 10 February 2017

Sightly/ HTL Tips

 Sightly Tips

For SIghtly Tutorial Visit our help page : http://aem-cq-tutorials.blogspot.com/p/htl-home.html


? Sightly comparing a string value
Say we have a string 'heroType' and having some values. We need to test its value to 'AL', we can use below code to compare it.

<div data-sly-test="${style.getHeroType == 'AL' }">Hello</div>

? Check a list's(formatList) size using below code
<sly data-sly-test.emptysize ="${subcategoryUse.formatList.size > 0 }" />

--------------Similar Posts:----------
-------------------------------------------

? Check a list item count is greater than zero in sightly
Java List : shopList; Use class : subcategoryUse

<sly data-sly-list.listItem="${subcategoryUse.shopList}">
 <sly data-sly-test.count="${listItemList.index > 0}">
 //Do your task here if count is more than zero
 </sly>

? Passing a value to Use  class from sightly:
 <sly    data-sly-use.integerUse="${'com.....IntegerUse' @ text=item}">

 And in Use class IntegerUse.java read it as,
 String text = get("text", String.class);

 ? Iterating over a list
 Java List: firstList

 <sly data-sly-list="${quantumUse.firstList}">
 //Use list item here using '{item}'
 </sly>

 ? When a Java list contains an object holding multiple values, each value can be retrieved as below
 <a x-cq-linkchecker="valid" href="${listItem.getUrl}.html"><img src="${listItem.getitemImage}" class="image-responsive"></a>
 or
 <p>${listItem.getpriceInteger} <span>${listItem.getpriceDecimal}</span>

 ? Test multiple items in sightly

 <sly data-sly-test="${listItem.selectLogo && listItem.displayLogo}">
 OR
<div data-sly-test="${listItem.selectCategoryBadge.length > 0 && listItem.displayCategoryBadge}"