Expand my Community achievements bar.

SOLVED

Use global object 'resolver' in sightly

Avatar

Level 2

Hi All,

 

I want to check if the resource exists or not using the sightly code. I was going through the documentation which consists of the global objects used in sightly : https://experienceleague.adobe.com/docs/experience-manager-htl/content/global-objects.html?lang=en.

I understand, there is a global object 'resolver' but no document how to use it.

 

Can someone please share the documentation or an example of how to use it in sightly code to get a resource.

 

Thanks in advance!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi Hiral,

 

Sightly is HTML expression/templating system. It's all about presentation not complex algo's or service calls.

 

You can invoke Sling Model that will do this check for you.

 

Indeed, you should create SlingModel to do all the logical checks and use Sightly to simply print out model with minimal amount of logic.

 

Regards,

Peter

View solution in original post

6 Replies

Avatar

Community Advisor

@hiral-vasa 

Sightly/HTL Variables
AEM Backend Objects
Documentation
component
com.day.cq.wcm.api.components.Component
componentContext
com.day.cq.wcm.api.components.ComponentContext
currentDesign
com.day.cq.wcm.api.designer.Design
currentNode
javax.jcr.Node
currentPage
com.day.cq.wcm.api.Page
currentSession
javax.servlet.http.HttpSession
currentStyle
com.day.cq.wcm.api.designer.Style
designer
com.day.cq.wcm.api.designer.Designer
editContext
com.day.cq.wcm.api.components.EditContext
log
org.slf4j.Logger
out
java.io.PrintWriter
pageManager
com.day.cq.wcm.api.PageManager
reader
java.io.BufferedReader
request
org.apache.sling.api.SlingHttpServletRequest
resolver
org.apache.sling.api.resource.ResourceResolver
resource
org.apache.sling.api.resource.Resource
resourceDesign
com.day.cq.wcm.api.designer.Design
resourcePage
com.day.cq.wcm.api.Page
response
org.apache.sling.api.SlingHttpServletResponse
sling
org.apache.sling.api.scripting.SlingScriptHelper
slyWcmHelper
com.adobe.cq.sightly.WCMScriptHelper
wcmmode
com.adobe.cq.sightly.SightlyWCMMode
xssAPI
com.adobe.granite.xss.XSSAPI
documentation

 

Avatar

Level 2

Thanks @Manu_Mathew_  for a quick reply.

These are the documentation for class/API. I am looking for the global object 'resolver' to be used in sightly code. 

It would be really helpful if you could share a few examples to validate if the resource exists or not using this global objects.

 

Thanks,

Hiral

Avatar

Level 2

Is there a way to do resolver.getResource(path) in sightly? 

Avatar

Community Advisor

@hiral-vasa I have not used it yet, however, may be you can try something like -

<sly data-sly-test="${resource['image/jcr:primaryType']}">
<sly data-sly-resource="${'image' @ resourceType='test/components/content/image'}"></sly>
</sly>

or use the methods using member variables for eg. if "resolver" has a getter method "getResource"  then you can probably use "${resolver.resource}". However, is recommended to use a sling model for such operations.

Avatar

Correct answer by
Community Advisor

Hi Hiral,

 

Sightly is HTML expression/templating system. It's all about presentation not complex algo's or service calls.

 

You can invoke Sling Model that will do this check for you.

 

Indeed, you should create SlingModel to do all the logical checks and use Sightly to simply print out model with minimal amount of logic.

 

Regards,

Peter

Avatar

Community Advisor

When implementing AEM component's you should be organized with coding standards. The front-end, Sightly(.html), should only focus on the VIEW. While the backend, Sling Model should handle complicated computation logic as the MODEL. If you would like to compare the JCR rather a resource exist, I suggest you do it from the backend; here in the backend, if you have complicated logic, you can write unit tests to ensure that the code coverage is set the to expectation of your organization.

 

Using Sling Model annotations, you can obtains the resoureceResolver, which can resolve and check wether the other resourceExists. If the path that you are trying to check is living in an restricted area in the AEM JCR file path, then I would recommend you to use a SystemUser to obtain the resourceResolver to resolve the resource; follow the articles below to programmatically configure a system user for your AEM environment.

Checkout these blog articles below:
Sling Model Cheat Sheet - AEM Sling Model Injectors Annotations Cheat Sheet Reference Guide - Sourced Code

Create AEM System Users with Code -  Create System | Service Users in AEM 6.5 with Code Configurations - Sourced Code