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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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, example of using the 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
Got Questions?

If you have any comments or topics that you would like me to cover, please leave a comment below.





Hello, I am an enthusiastic Adobe Community Advisor and a seasoned Lead AEM Developer. I am currently serving as an AEM Technical Lead at MNPDigital.ca, bringing over a decade of extensive web engineering experience and more than eight years of practical AEM experience to the table. My goal is to give back to the AEM Full Stack Development community by sharing my wealth of knowledge with others. You can connect with me on LinkedIn.

4 thoughts on “AEM Global Objects for Backend and Front-end Sightly (HTL) Development

Leave a Reply

Your email address will not be published. Required fields are marked *


Back To Top