Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Get selector value from sightly html page [Bit Urgent : Any thoughts on this will be helpful]

Avatar

Level 9

Hi All,

Via Java code, we are able to get the selectors by "request.getRequestPathInfo().getSelectors()" . On a component html[making use of sightly code], how do I retrieve the selector value on that page.

Any thoughts on this will be really helpful.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Or you can just use Sling Model instead of Sightly Model

 

@Model(adaptables = SlingHttpServletRequest.class) public class SelectorCheck{ @Inject SlingHttpServletRequest request; private boolean enabled = false; @PostConstruct public void postConstruct() { if (request != null) { List<String> selectors = Arrays.asList(request.getRequestPathInfo().getSelectors()); enabled = selectors.contains("abc"); } } public boolean isEnabled() { return enabled; } }

View solution in original post

7 Replies

Avatar

Level 10

For this use case, make use of a java bean. 

Avatar

Level 2

use a request scoped sling model and use that to get the selector and then use that on your sightly script.

Avatar

Level 10

Hi, 

Find the example below 

 

Sightly HTML

<div data-sly-use.selectorCheck="com.foo.SelectorCheck"> <div data-sly-test="!selectorCheck.enabled" >Selector Content</div> </div>

Sightly Model

public class SelectorCheck extends WCMUse { @Inject SlingHttpServletRequest request; private boolean enabled = false; @Override public void activate() throws Exception { if (request != null) { List<String> selectors = Arrays.asList(request.getRequestPathInfo().getSelectors()); enabled = selectors.contains("abc"); } } public boolean isEnabled() { return enabled; } }

Avatar

Correct answer by
Level 10

Or you can just use Sling Model instead of Sightly Model

 

@Model(adaptables = SlingHttpServletRequest.class) public class SelectorCheck{ @Inject SlingHttpServletRequest request; private boolean enabled = false; @PostConstruct public void postConstruct() { if (request != null) { List<String> selectors = Arrays.asList(request.getRequestPathInfo().getSelectors()); enabled = selectors.contains("abc"); } } public boolean isEnabled() { return enabled; } }

Avatar

Level 9

Hi All,

Thank you for your replies. Trying on it.

Avatar

Level 9

Hi All,

Apologies for the delay in responding back.

Using Sling model helped. But somehow, I do not get an option to mark the answer as resolved.