You can pass data parameters from Sightly HTL component to the WCMUsePojo backend in a very simple way. You can simply call the get(String name, Class
I created the code below to showcase a real production scenario. After a selection from the TouchUI pathfinder, we are given a path of /content/sourcedcode/en/home. From sightly, we must append a .html, but when the path was not chosen with the TouchUI pathfinder with the value set manually to https://sourcedcode.com, no .html extensions should be added.
The example below showcases how we can logically insert the .html extensions based on conditions.
Sightly HTL
1 2 3 4 5 6 7 8 9 10 11 12 | // expect /content/sourcedcode/home.html <a class="title-text" data-sly-use.linkResolve="${'com.sourcedcode.core.utils.use.LinkResolve' @ href='/content/sourcedcode/home'}" href="${linkResolve.href}" target="${linkResolve.target}" rel="noopener"> ${linkResolve.href} = ${linkResolve.target} </a> // expect https://sourcedcode.com <a class="title-text" data-sly-use.linkResolve="${'com.sourcedcode.core.utils.use.LinkResolve' @ href='https://sourcedcode.com'}" href="${linkResolve.href}" target="${linkResolve.target}" rel="noopener"> ${linkResolve.href} = ${linkResolve.target} </a> // expect https://sourcedcode.com <a data-sly-use.linkResolve="${'com.sourcedcode.core.models.LinkResolve' @ href=properties.href, target=properties.target}" href="${linkResolve.href}" target="${linkResolve.target}" rel="noopener"> ${linkResolve.href} </a> |
WCMUsePojo Class : LinkResolve.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package com.sourcedcode.core.utils.use; import com.adobe.cq.sightly.WCMUsePojo; import java.util.regex.Pattern; public class LinkResolve extends WCMUsePojo { private String href = ""; private String target = ""; @Override public void activate() { href = get("href", String.class); target = get("target", String.class); if (href == null || href.isEmpty()) return; boolean isLinkExternal = Pattern.compile("^https?://.*").matcher(href).matches(); boolean isLinkInternalAsset = Pattern.compile(".*\\.(\\w){1,5}$").matcher(href).matches(); if (!isLinkExternal && !isLinkInternalAsset) { href = (href.endsWith(".html") || href.contains("?")) ? href : href + ".html"; } if (target == null || target.isEmpty()) target = "_self"; } public String getHref() { return href; } public String getTarget() { return target; } } |