AEM Developer Learning
AEM Developer Learning
▼ 2018 (4)
Deep Dive in HTL/Sightly in AEM 6.3 ▼ March (1)
Deep Dive in HTL/Sightly in AEM 6.3
Hello Everyone,
► February (2)
The term “sightly” or “HTL” is not very new to all of us. We have been developing our ► January (1)
components in HTL in place of “JSP” since long.
► 2017 (32)
But as it is little hard to remember all the syntax, In this blog I am trying to cover all the
possible block statements in HTL/sightly with use cases as well. Followers
While included parsys in the Page Component, you need to add init.jsp in the page.
<sly data-sly-include="/libs/wcm/core/components/init/init.jsp"/>
Follow
Note: Don’t add init.jsp on every page or component, where you need parsys to
include. Just add it in base page component and use it everywhere.
Follow by Email
Add caption
But if you put the same statement with data-sly-repeat, it will iterate the complete
structure including the conditional statement as well.Example:
Add caption
If you want to have the same output from repeat, like list, you need to write something
like:
<ul>
<li data-sly-repeat.child="${ currentPage.listChildren }">
${child.name}
</li>
</ul>
<ul data-sly-list="${currentPage.listChildren}"
<li data-sly-test="${itemList.even}">${item.title}</li>
</ul>
<p data-sly-repeat.mapItem="${myMap}">
<span>key: ${ mapItem }</span>
<span>value: ${myMap[mapItem]}</span>
</p>
Note:Always use the identifier instead of default “item” variable for list of block and
repeat statements.
data-sly-use "is used to add JS/JAVA". You declare component-beans with this
statement for instance:
<sly data-sly-use.example="com.example.project.helloWorld"/>
${example.path}
Always cache test block statement results in an identifier if it repeats itself. There
may be a need of using a value at various place in a single HTML, so it’s good to
have a variable having that value and use it at all places.
<sly data-sly-test.path="${currentPage.path}"/>
Sly tag don’t let be the statements the part of DOM and clears out the rendered
HTML and get rid of additional divs. You can also use data-sly-unwrap to remove the
element from DOM.
Note: Always use existing HTML elements for your block statements if possible.
data-sly-resource in HTL/Sightly
Includes the result of rendering the indicated resource through the sling resolution
and rendering process. It works completely on the concept of sling resolution.
By default, The AEM Decoration Tags are disabled, the decoration tagName option allows
to bring them back and the cssClassName to add classes to that element.
<article data-sly-resource="${'path/to/resource' @ decorationTagName='span',
cssClassName='className'}"></article>
data-sly-include in HTL/Sightly
The element on which a data-sly-include has been set is ignored and displayed.
<!--/* Following will simply output the rendered content of the template,
the complete <div> element will be ignored.*/-->
<div id=”test-one” class=”test-two” data-sly-include=”template.html”>Foo </div>
<!--/* Outputs only the result of the template. */-->
Try to unwrap all includes resources and other HTML elements that are not part of the markup.
data-sly-template in HTL/Sightly
Template blocks can be used like function calls: in their declaration, they can get parameters,
which can then be passed when calling them.
Static template that has no parameters:
<template data-sly-template.one>blah</template>
<div data-sly-call="${one}"></div>
The scope of the data-sly-call statement isn't inherited by the data-sly-template block.
To pass variables, they must be passed as parameters:
<template data-sly-template.two="${@ title, resource='The resource of the parent node'}">
<h1>${title}</h1>
<p>Parent: ${resource.name}</p>
</template>
<div data-sly-call="${two @ title=properties.jcr:title, resource=resource.parent}"></div>
When templates are located in a separate file, they can be loaded with data-sly-use:
<div data-sly-use.lib="templateLib.html" data-sly-call="${lib.one}"></div>
When some parameters are missing in a template call, that parameter would be
initialised to an empty string within the template.
Labels: AEM, all-about-aem, CQ, data-sly-include, data-sly-list and data-sly-repeat, data-sly-resource, data-sly-
template, data-sly-test, HTL, sightly
Powered by Blogger.