Hi,
I am trying to access currentStyle properties from within a SlingModel. The SlingModel is being called from a HTL component using data-sly-use that has a functional design_dialog and accessible currentStyle.
This was the only suggestion I could find using google:
@Inject @Source("script-bindings")
private Style currentStyle;
and then
@PostConstruct
public void init() {
String startLevel = currentStyle.get("startLevel",String.class);
}
This isn't working. Can anyone tell me what I am doing wrong??
Thank you!
Ian
Solved! Go to Solution.
Irrespective of adapting to use Resource or Slinghttpservletrequest.
With latest sling jar, You can use below code to get access
@ScriptVariable
private Style currentStyle;
You can refer : https://github.com/Adobe-Marketing-Cloud/aem-core-wcm-components/blob/d4a9c2c7785f5d50e91dd2ebe145ac...
Update:
I decided to extend WCMUse instead, this class has access to getCurrentStyle(). I am still interested if anyone has a way to make this work with a sling model though!
Views
Replies
Total Likes
I would suggest you to use the above one . It works for me
Are you using @Model(adaptables = SlingHttpServletRequest.class)
@Model(adaptables = SlingHttpServletRequest.class) public class SampleModel{ private final Logger LOG = LoggerFactory.getLogger(getClass()); @Inject SlingHttpServletRequest request; @Inject private Page currentPage; @Inject @Source("script-bindings") private Style currentStyle;
Views
Replies
Total Likes
No I wasn't using @Model(adaptables = SlingHttpServletRequest.class), I had
@Model(adaptables = Resource.class) instead. that was probably my issue then?
Views
Replies
Total Likes
Can you try with SlingHttpServletRequest.class and let me know if this works.
Irrespective of adapting to use Resource or Slinghttpservletrequest.
With latest sling jar, You can use below code to get access
@ScriptVariable
private Style currentStyle;
You can refer : https://github.com/Adobe-Marketing-Cloud/aem-core-wcm-components/blob/d4a9c2c7785f5d50e91dd2ebe145ac...
Excellent answers Praveen and Veena!
Hi Veena,
Sorry for the late reply.
I managed to try this with a new model that i created and it worked beautifully. Thank you for finding my problem!!!
Regards,
Ian