December 28, 2020
Estimated Post Reading Time ~

AEM Global Objects for Backend and Front-end Sightly (HTL) Development

While working on an AEM project, we can speed up the process of development by utilizing the global objects offered by AEM Framework.

1. AEM Backend, an example of using global objects
  • The global objects are accessible by OSGI’s dependency injection annotation; @inject.
  • GET, SET, and other methods can be invoked by the POJO class.
import com.day.cq.wcm.api.Page;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.logging.Logger;

@Model(adaptables = {SlingHttpServletRequest.class, Resource.class},
resourceType = ExampleComponent.RESOURCE_TYPE)
public class ExampleComponent {

static final String RESOURCE_TYPE = "sourcedc/components/structure/examplecomponent";

@ScriptVariable
private Logger log;

@ScriptVariable
private SlingHttpServletResponse response;

@SlingObject
private SlingHttpServletRequest request;

@ScriptVariable
private Page currentPage;

@ScriptVariable
private Resource resource;

@PostConstruct
private void init() {
String componentsProp = resource.getValueMap().get("subtitleText", String.class);
String currentPagePath = currentPage.getPath();
String userID = request.getParameter("userid");
String localeCountry = response.getLocale().getCountry();

// setting json response
response.setContentType("application/json");
String contentType = response.getContentType();
log.info("Example component initiated.");
}

}


2. AEM Front-End, an example of using global objects, stored in reserved variables:
  • The global objects are accessible by using unique variables (defined below).
  • Only GET methods can be invoked by Sightly/HTL.
  • While previewing the java documentation, only the get methods can be used. While using the global object, exclude the “get” prefix.
Current Page Path : ${currentPage.path} <!-- javadocs : getPath(); -->

Current Page Name : ${currentPage.name} <!-- javadocs : getName(); -->

response country : ${response.locale.country} <!-- javadocs : getLocale().getCountry(); -->

response type : ${response.contentType} <!-- javadocs : geetContentType(); -->

component's property subTitleText : ${properties.subTitleText}

<!-- loop through all children page's of current page -->
<div data-sly-list="">
<p>${child.name}</p>
</div>

// this article does not cover the usage of the Sightly(HTL) syntax. For a quick reference, check out this document:
https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md


3. Mapping of Sightly/HTL Variables and AEM Backend Objects:

Sightly/HTL Variables
AEM Backend Objects
Documentation
component
com.day.cq.wcm.api.components.Component
componentContext
com.day.cq.wcm.api.components.ComponentContext
currentDesign
com.day.cq.wcm.api.designer.Design
currentNode
javax.jcr.Node
currentPage
com.day.cq.wcm.api.Page
currentSession
javax.servlet.http.HttpSession
currentStyle
com.day.cq.wcm.api.designer.Style
designer
com.day.cq.wcm.api.designer.Designer
editContext
com.day.cq.wcm.api.components.EditContext
log
org.slf4j.Logger
out
java.io.PrintWriter
pageManager
com.day.cq.wcm.api.PageManager
reader
java.io.BufferedReader
request
org.apache.sling.api.SlingHttpServletRequest
resolver
org.apache.sling.api.resource.ResourceResolver
resource
org.apache.sling.api.resource.Resource
resourceDesign
com.day.cq.wcm.api.designer.Design
resourcePage
com.day.cq.wcm.api.Page
response
org.apache.sling.api.SlingHttpServletResponse
sling
org.apache.sling.api.scripting.SlingScriptHelper
slyWcmHelper
com.adobe.cq.sightly.WCMScriptHelper
wcmmode
com.adobe.cq.sightly.SightlyWCMMode
xssAPI
com.adobe.granite.xss.XSSAPI
documentation


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.