HTL (HTML Template Language) Syntax in AEM

HTL (HTML Template Language) Syntax in AEM

When working in Adobe Experience Manager (AEM), you’ll first encounter HTL (HTML Template Language), also known as Sightly. As someone who has worked with AEM, I’ve found HTL to be an incredibly useful language for writing clean, efficient, and secure server-side templates. Today, I’ll dive into some basic HTML syntax and explain why it’s essential, using some practical code as well as app examples.

What is the meaning of HTL in AEM?

HTL, introduced by Adobe, was designed to replace JSP (JavaServer Pages) for AEM templates. HTL provides a secure and streamlined way to integrate dynamic content into HTML pages, ensuring that security concerns such as XSS (Cross-Site Scripting) are handled automatically. HTL is beginner-friendly and allows developers to focus on the presentation layer without diving deep into Java code.

Breaking Down the Code: Static and Dynamic Lists in HTL

Let’s take a look at some concrete yet simple examples:

Static List

<ul>
  <li>Item1</li>
  <li>Item2</li>
  <li>Item3</li>
  <li>Item4</li>
  <li>Item5</li>
</ul>

This part of the code is straightforward HTML. It defines a static list with five items. Each list item (<li>) contains a hardcoded value like “Item1”, “Item2”, and so on. While this approach works well when you have a fixed set of list items, it doesn’t scale well if you need to generate a list dynamically based on the content or user interaction.

Dynamic List with HTL

<ul data-sly-list="${[1,2,3,4]}">
  <li>${item}</li>
</ul>

This part of the code demonstrates how HTL can be used to dynamically generate a list. Let’s break this down step by step:

  1. data-sly-list: This HTL attribute iterates over an array or list. In this example, the array is [1, 2, 3, 4]. HTL will loop over each element in the array and render it in the HTML. Instead of writing out each item manually like in the static list, HTL does the heavy lifting for you.
  2. ${item}: Inside the <li> tag, ${item} is a placeholder for the current element in the array during the loop. On each iteration, item represents the value of the array’s current element. In this case, it will render 1, 2, 3, and 4 into the list.

Let’s look at what happens when this HTL code is executed. AEM will output:

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
</ul>

Just like that, HTL generates the list dynamically without requiring you to write out each item individually.

What is HTL used for?

As simple as this example might seem, it demonstrates several powerful concepts that make HTL an essential tool in AEM development:

1. Dynamically Generated Content

In real-world AEM applications, content is rarely static. Whether you’re working with product listings, blog posts, or any other type of data, content is typically pulled from a repository or database. HTL’s ability to loop over data structures like arrays or lists allows you to display this dynamic content effortlessly.

For example, imagine you need to render a list of products stored in an AEM content repository. Instead of manually hardcoding each product into the HTML, HTL allows you to iterate over the data, which ensures that your content stays up-to-date automatically as the data changes.

2. Separation of Concerns

HTL promotes a clear separation between the presentation layer (the HTML) and the business logic. This separation is crucial for clean, maintainable code. HTL templates handle the view logic, while AEM’s Java backend or Sling Models provide the data. This makes it easier to work collaboratively in a team where frontend developers focus on HTL and HTML, while backend developers manage the business logic in Java.

3. Increased Security

One of the main reasons Adobe introduced HTL was to enhance security. When you output dynamic content in HTL using ${item} like in the example, HTL automatically escapes special characters to prevent XSS attacks. This is critical in today’s web development, where security vulnerabilities can easily be exploited. The best part is that you don’t have to worry about manually escaping content; HTL does it for you.

4. Efficiency

Imagine trying to render hundreds or even thousands of list items manually in HTML. That would be incredibly tedious and error-prone. HTL simplifies this process by letting you loop over your data efficiently. It saves time and reduces the risk of errors in your code.

In addition, HTL performs well when rendering large data sets. The templating engine is optimized for server-side performance, ensuring that your AEM pages load quickly, even with dynamic content.

Going Beyond the Basics: More HTL Features

While the data-sly-list attribute is one of the more common HTL features, HTL offers several other useful attributes and directives. Here are a few that I’ve found particularly helpful:

1. data-sly-test

This directive is used for conditional rendering. For example, if you only want to display an HTML block if a certain condition is met, you can use data-sly-test:

<div data-sly-test="${items.length > 0}">
  <ul>
    <li data-sly-list="${items}">${item}</li>
  </ul>
</div>

This will only render the list if items has one or more elements.

2. data-sly-use

The data-sly-use directive allows you to instantiate and use Java objects within your HTL template. For example, if you have a Sling Model that fetches data from AEM’s repository, you can use it in your HTL like this:

<div data-sly-use.product="com.mycompany.ProductModel">
  <p>Product Name: ${product.name}</p>
</div>

This enables you to access data in your backend Java code directly in your HTL template.

3. data-sly-repeat

data-sly-repeat is similar to data-sly-list, but it allows you to repeat entire blocks of HTML. This is useful when you need to repeat a more complex block of markup based on your data.

Why Should You Learn HTL?

From my experience working with AEM, learning HTL is one of the best investments you can make as a developer. Here’s why:

  • Simplicity: HTL is incredibly easy to learn, especially if you’re already familiar with HTML. The learning curve is shallow, and even developers new to AEM can start writing useful templates quickly.
  • Security: HTL’s built-in security features, such as automatic escaping, make it a safe choice for dynamic content rendering.
  • Maintainability: Clean separation of concerns makes your code easier to maintain. HTL keeps the presentation layer free from business logic, allowing you to update the frontend without touching the backend.
  • Community and Documentation: HTL is well-documented, and the AEM community is active and supportive. This means you’ll have plenty of resources to learn from and troubleshoot any issues you may encounter.

Simple app simulation

Here is a simple app simulation created with HTML, CSS, and JS that emulates certain HTL features:

You can find the app source code here: https://github.com/NoToolsNoCraft/AEM-HTL-Simulator

Key Features of the Simulator

  1. Input Length Limitation:
    • The code limits the total input length to 20 characters and ensures that each individual item can only be 13 characters long. This mirrors common validation practices in AEM forms, where user input is often constrained for usability and data integrity.
  2. Dynamic Item Management:
    • When users input items separated by commas, the code dynamically creates a list of these items and appends them to a display list. This simulates how AEM HTL can render dynamic content based on user input.
  3. Trimming and Formatting:
    • The code trims whitespace and removes empty items, which is a common best practice in both client-side scripting and server-side rendering (like HTL) to ensure clean data processing.
  4. Event Handling:
    • The use of event listeners for input and button clicks reflects how user interactions are often handled in AEM using HTL and JavaScript. This approach enhances the application’s interactivity.
  5. Remove Button Functionality:
    • Each list item includes a remove button, allowing users to delete items from the list dynamically. This is akin to how HTL templates might allow users to modify displayed content based on their actions.

Scroll to Top