Get selector value from sightly html page [Bit Urgent : Any thoughts on this will be helpful] | Community
Skip to main content
Level 9
February 10, 2016
Solved

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

  • February 10, 2016
  • 7 replies
  • 9966 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Lokesh_Shivalingaiah

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; } }

7 replies

smacdonald2008
Level 10
February 10, 2016

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

Level 8
February 10, 2016
${request.requestPathInfo.selectors[0]}
Level 2
February 10, 2016

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

Lokesh_Shivalingaiah
Level 10
February 10, 2016

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; } }
Lokesh_Shivalingaiah
Lokesh_ShivalingaiahAccepted solution
Level 10
February 10, 2016

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; } }
askdctmAuthor
Level 9
February 10, 2016

Hi All,

Thank you for your replies. Trying on it.

askdctmAuthor
Level 9
March 1, 2016

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.