AEM Slightly Cheatsheet Part 1
AEM Slightly Cheatsheet Part 1
Cheatsheet
Part 1
Tushar Bhite
HTL (SIghtly) Introduction :
HTL Intro :
i). Expression language : Used to insert pieces of content into the rendered
markup.
ii). Block Statements: Used to define statements over blocks of markup
(conditions/iterations)
Advantages:
Increased Security
HTL gets compiled into Java Servlets, during this process the expressions and
the HTL data attributes are both evaluated entirely server-side, thus the view
source option of HTML page does not show anything.
Reduced Costs
HTL Provides increased security, simplified development and improved team
collaboration, faster time to market (TTM), reduced e ort and lower total cost of
ownership (TCO).
Simplified Development
The HTML Template Language is easy to learn for a developer who knows basic
HTML and its syntax and features are purposely limited to ensure that it stays
simple and straight-forward.
HTL Syntax :
HTL Syntax
HTL is strict with the syntax. Considering the types there are two types of
HTL syntax.
Block Statements :
Basically they are custom data attributes added directly to existing HTML.
Block statements are prefixed with data-sly.
Expression Language :
HTL expressions are evaluated at run time and their value is injected into the
outgoing HTML stream.
HTL expressions are delimited by characters ${ and }.
HTL Variables
HTL Literals(Boolean, Number,Strings,Arrays)
HTL Operators(Logical Operators,Conditional (ternary) Operator, Comparison
Operators, Grouping parentheses)
HTL Options(String Formatting,Internationalization,Array Join,Display Context)
HTL Options :
HTL Options
Options are used with block statements, Symbol @ starts the option expression.
Options can have a value, which may be either a variable or a literal. Options are
mainly used for String Formatting, Internationalization, Array Join.
String Formatting :
${'Page {0} of {1}' @ format=[current, total]}
Internationalization :
${'Page' @ i18n, hint='Direction for translators', source='user'}
Array Join :
${['1', '2'] @ join='; '}
Display Context :
HTL will automatically detect the context of expressions and escape them
appropriately to ensure the security and avoid XSS vulnerability. The context
behavior may need to be adjusted as our coding needs. This can be explicitly set
to override the automatic behavior of HTL. HTL display context refers to its
location within the structure of the HTML page.
● text
● html
● attribute
●uri
● number
● attributeName
● elementName
● scriptToken
● scriptString
● scriptComment
● styleToken
● styleString
● styleComment
● unsafe
//Safely output the markup
${properties.richText@context='html'}
color: ${red @ context=’scriptToken’} //No spaces in css
fonntfamily:${bold italic strikethrough @ context=’styleString’} //spaces in
between values
HTL Variables :
HTL Variables
1. properties component,componentContext,currentDesign
2 pageProperties currentNode, currentPage, currentSession,
. InheritedPageProperti currentStyle, designer, editContext, log out,
3 es pageManager, reader, request resolver, resource,
. resourceDesign, resourcePage, response, sling,
slyWcmHelper,wcmmode,xssAPI.
Accessing :
Thumb rule for valid names
start with a letter (A-Z and a-z), or an underscore (_), and subsequent characters
can also be digits (0-9) or colon (:).
For eg:
<p>${'John\'s car is "awesome !", Maria said'}</p>
Arrays :
An array is a collection of values that can be represented with a common
name. An array will have an index ans ordered.
Samples:
//Array of 2 elements
${utilityArray[2]}
// Array of 5 digits
${[1,2,3,4,5]}
//print an array by names separated
by ':' ${utilityArray.names @ join=':'}
Logical Operators :
Logical Operators are used with Boolean values, where the operator returns the
value of one of the specified operands. While using with non-Boolean values,
they may return a non-Boolean value. They are of 3 types.
● AND:<Pdata-sly-test="${properties.jcr:title&&
properties.jcr:description}">DISPLAY</p>
● OR:<Pdata-sly-test="${properties.jcr:title||
properties.jcr:description}">DISPLAY</P>
● NOT:<pdata-sly-test="${!currentPage.hasChild}">DISPLAY</p>
Conditional/Ternary Operators :
Conditional operator can be used to define conditions within expressions.
${condition ? resultExpressionWhenTrue : resultExpressionWhenFalse}
Comparison Operators :
Comparison Operators(equality and inequality operators) used to compare
identical type operands.
● Strings-equal-havingthesamesequenceofcharacters.
● Numbers-equalhavingthesamevalue
● Booleans-equal-whenbotharetrueorbotharefalse.
● Nullorundefinedvariables-equal-tothemselvesandtoeachother.
Samples:
Grouping Parenthesis :
As in other languages HTL supports bracketing to ensure the preference of
operation.
<data-sly-test/> Validates and conditionally removes the host element and it's
content. Returned 'False' removes the element; Returned value 'true' retains the
element.
For eg:
Here the h1 element and its content will only be rendered if 'display' is true.
<h1 data-sly-test="${display}">text</h1>
For eg:
HTL sly-use :
<div data-sly-use.nav="org.company.Navigation">${nav.foo}</div>
JavaScript
<div data-sly-use.nav="navigation.js">${nav.foo}</div>
HTL sly-unwrap :
HTL sly-unwrap
Trims the html element from the generated markup.
For eg:
<p data-sly-use.sort="sorting.js" data-sly-unwrap>Sort Text</p>
HTL sly-text
HTL sly-text
Inserts the content of its host element with the specified text by replacing it.
For eg:
. Prints the current page title, still helpful for searching in html source using
term 'My Page Title'
HTL sly-attribute
HTL sly-attribute
For eg:
HTL sly-element
Used to replaces the element name of the host element of html tags.
For eg:
This replaces the h6 with the value of headerLevel. (Say the returned value for
'headerLevel' is h1, the <h6> will be replaced to h1)
HTL sly-repeat :
HTL sly-repeat
This tag helps to repeat an element multiple times based on the list which
is specified.
For eg:
<div data-sly-repeat="${currentPage.listChildren}">${item.name}</div>
The di erence with <data-sly-list/> is that list needs a container element where
repeat does not require it.
HTL sly-list :
HTL sly-list
List tag repeats the content of the host element for each enumerable property
in the provided object.
For eg:
<h6 data-sly-list="${currentPage.listChildren}">
<dt>index: ${itemList.index}</dt>
<dd>value: ${item.title}</dd>
</h6>
Where 'item' is the current item in the iteration & itemList is the object holding
the properties.
ItemList Properties:
Property Description
middle: true if the current item is neither the first nor the last
<h6 data-sly-list.child="${myList}">
<dt>key: ${child}</dt>
<dd>value: ${myList[child]}</dd>
</h6>
HTL sly-resource ;
HTL sly-resource
Helps to include the result of a resource from a path specified. The sling
resolution and rendering process will process the indicated resource from
the path and the result will be included.
<file data-sly-resource="path/to/myresource"></file>
We can mention various methods for referring resource path. Shown below.
● "${ @ path='path/to/myresource'}"
● "${'myresource' @ prependPath='my/path'}"
● "${'my/path' @ appendPath='myresource'}"
HTL sly-include :
HTL sly-include
Helps to replace the content of the host element with the markup generated by
the specified HTML template file (It can be any of the HTL, JSP, ESP formats.)
<section data-sly-include="path/to/jsptemplate.jsp"></section>
HTL sly-template :
HTL sly-template
Used to define a template where the host element and its content are not
output by HTL. Always used with <data-sly-call/>
For eg:
<template data-sly-template.mytemplate>TEST</template>
<div data-sly-call="${mytemplate}"></div>
HTL sly-call
HTL sly-call
Used to call a template defined with <data-sly-template/>. The content of the
called template replaces the content of the host element of the call.
For eg:
<template data-sly-template.mytemplate>TEST</template>
<div data-sly-call="${mytemplate}"></div>
HTL Formatting :
HTL allows rich way of URL Formatting.
HTML Extension
The latest AEM versions support numbers and dates formatting which includes
timezone and locale formatting. We don't have to write custom code any more.
The syntax for formatting is format is specified first, then the value which needs
to be formatted.
For eg
Date formatting
Number formatting
For eg.
<sly data-sly-use.settings="com.adobe.examples.htl.core.hashmap.Settings"
data-sly-include="${ 'productdetails.html' @
requestAttributes=settings.settings}" />
@Override
public void activate() throws Exception {
settings.put("order", "ascending");
}
}
@Model(adaptables=SlingHttpServletRequest.class
) public class ProductSettings {
@Inject @Optional @Default(values="empty")
public String order;
}
Thank You
Tushar Bhite