AEM - Sightly Elements
In the earlier blogs, we have seen when use data-sly-test using a div tag, div tag is also displayed in the html content.
To avoid unnecessary wrappers around the actual HTML content, we can directly use the required HTML markup or use the sly element directly.
For Example:
<div data-sly-test.pageLinkReference="${properties.cq:pageLinkRef}">
<a href="${pageLinkReference}">${page.title}</a>
</div>
Output: (We can see the additional div tag around the anchor elements.
<div>
<a href="/content/sampleproject/fr.html">This text is populated from the component</a>
</div>
To remove the additional div use the below example with sly elements
<sly data-sly-test.pageLinkReference="${properties.cq:pageLinkRef}">
<a href="${pageLinkReference}">${page.title}</a>
</sly>
Output:
<a href="/content/sampleproject/fr.html">This text is populated from the component</a>
<a href="${properties.linkTo}.html">${properties.text}</a>
</div>
data-sly-text
This element replaces the content of its host element with the specified text.
<p data-sly-text="${properties.text}">Lorem ipsum</p>
Update attribute values using data-sly-attribute
<div title="Lorem Ipsum" data-sly-attribute.title="${properties.text}"></div>
data-sly-repeat
Use this element to repeat the HTML element around the object in a list
Example:
<div data-sly-repeat="${currentPage.listChildren}">${item.name}</div>
To avoid unnecessary wrappers around the actual HTML content, we can directly use the required HTML markup or use the sly element directly.
For Example:
<div data-sly-test.pageLinkReference="${properties.cq:pageLinkRef}">
<a href="${pageLinkReference}">${page.title}</a>
</div>
Output: (We can see the additional div tag around the anchor elements.
<div>
<a href="/content/sampleproject/fr.html">This text is populated from the component</a>
</div>
To remove the additional div use the below example with sly elements
<sly data-sly-test.pageLinkReference="${properties.cq:pageLinkRef}">
<a href="${pageLinkReference}">${page.title}</a>
</sly>
Output:
<a href="/content/sampleproject/fr.html">This text is populated from the component</a>
Data-sly-unwrap with conditional check to remove the HTML tags.
<div data-sly-test="${properties.linkTo}" data-sly-unwrap="${properties.displayText}"><a href="${properties.linkTo}.html">${properties.text}</a>
</div>
data-sly-text
This element replaces the content of its host element with the specified text.
<p data-sly-text="${properties.text}">Lorem ipsum</p>
Update attribute values using data-sly-attribute
<div title="Lorem Ipsum" data-sly-attribute.title="${properties.text}"></div>
data-sly-repeat
Use this element to repeat the HTML element around the object in a list
Example:
<div data-sly-repeat="${currentPage.listChildren}">${item.name}</div>
Change the default item and itemlist names to a different variable as below:
<dl data-sly-list.child="${currentPage.listChildren}">
<dt>index: ${childList.index}</dt>
<dd>value: ${child.title}</dd>
</dl>
Adding extension to a Link
<a href="${properties.linkTo @ extension = 'html'}">${properties.text}</a>
Comments
Post a Comment