HTML Template Language(Sightly)

The purpose of HTML Template Language (HTL), supported by Adobe Experience Manager (AEM), is to offer a highly productive enterprise-level web framework that increases security, and allows HTML developers without Java knowledge to better participate in AEM projects.

The HTML Template Language has been introduced with AEM 6.0, and takes the place of JSP (JavaServer Pages) as the preferred and recommended server-side template system for HTML. For web developers who need to build robust enterprise websites, the HTML Template Language helps to achieve increased security and development efficiency.


Interview Questions

Note: For more on sightly watch this video

The HTML Template Language has been introduced with AEM 6.0, and takes the place of JSP (JavaServer Pages) as the preferred and recommended server-side template system for HTML. Folowing are the benefits of HTML Template Language

  • Increased Security - The HTML Template Language increases the security of sites that use it in their implementation, as compared to JSP and to most other template systems, because HTL is capable of automatically applying the proper context-aware escaping to all variables being output to the presentation layer. HTL makes this possible because it understands the HTML syntax, and uses that knowledge to adjust the required escaping for expressions, based on their position in the markup. This will for instance result in expressions placed in href or src attributes to be escaped differently from expressions placed in other attributes, or elsewhere.

    While the same result can be achieved with template languages like JSP, there the developer must manually ensure that the proper escaping is applied to each variable. As a single omission or mistake on the applied escaping is potentially sufficient to cause a cross-site scripting (XSS) vulnerability, we decided to automate this task with HTL.

  • Simplified Development - The HTML Template Language is easy to learn and its features are purposely limited to ensure that it stays simple and straight-forward.
  • Reduced Costs - Increased security, simplified development and improved team collaboration, translates for AEM projects in reduced effort, faster time to market (TTM), and lower total cost of ownership (TCO).
Read more

Java Use-API - The HTML Template Language (HTL) Java Use-API enables a HTL file to access helper methods in a custom Java class. This allows all complex business logic to be encapsulated in the Java code, while the HTL code deals only with direct markup production. Read more

JavaScript Use-API - The HTML Template Langugae (HTL) JavaScript Use-API enables a HTL file to access helper code written in JavaScript. This allows all complex business logic to be encapsulated in the JavaScript code, while the HTL code deals only with direct markup production. Read more

Use SlingScriptHelper.getService() method (into Java Use-API class)
SampleService service=getSlingScriptHandler().getService(SampleService.class);
  • Attribute attribute - Use an array to make things easy. Example
         attrMap = {
         title: "myTitle",
         class: "myClass",
         id: "myId"
         }
          <div data-sly-attribute="${attrMap}"></div>       <div title="myTitle" class="myClass" id="myId"></div>
  • Element attribute - Replaces the element of the host element
  • Unwrap attribute - Remove the outer elements (i.e. HTML Tags) leaving just the output of the Expression)

Without having to specify anything, HTL provides access to all objects that were commonly available in JSP after including global.jsp. These objects are in addition to any that may be introduced through the Use-API.

Enumerable Objects :

These objects provide convenient access to commonly used information. Their content can be accessed with the dot notation, and they can be iterated-through using data-sly-list or data-sly-repeat.

Variable Name Description
properties List of properties of the current Resource.
Backed by org.apache.sling.api.resource.ValueMap

pageProperties List of page properties of the current Page.
Backed by org.apache.sling.api.resource.ValueMap
inheritedPageProperties List of inherited page properties of the current Page.
Backed by org.apache.sling.api.resource.ValueMap

Java-backed Objects :

Variable Name Description
component com.day.cq.wcm.api.components.Component
currentDesign com.day.cq.wcm.api.designer.Design
currentPage com.day.cq.wcm.api.Page
request org.apache.sling.api.SlingHttpServletRequest
resource org.apache.sling.api.resource.Resource
resourceDesign com.day.cq.wcm.api.designer.Design
resourcePage com.day.cq.wcm.api.Page
wcmmode com.adobe.cq.sightly.SightlyWCMMode
Read More

Sightly Offers below advantages over JSP for better development in AEM

  • Protection against cross-side scripting injection.
  • Easily development of AEM Projects by front-end developers.
  • Flexible and powerful templating and logic binding features
  • Need to write less code in Sightly thus productivity increases.
  • Wider range of implicit objects as compared to JSP.