Expand my Community achievements bar.

SOLVED

In AEM sightly check author or preview or publish instnace

Avatar

Level 1

We have a requirement to load an experience fragment in aem author instance (in all modes - edit, preview & disabled) using data-sly-resource approach where as in aem preview & publisher instances we want to load the experience fragment using Server Side Include (SSI) approach.

 

To achieve this I need to identify in the AEM Instance (author, preview or publish) in AEM Sightly Code using data-sly-test approach.

 

Please suggest me how to implement it.

 

@arunpatidar

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
5 Replies

Avatar

Correct answer by
Community Advisor

Hi @ravindrard 
You need to use author runmode here.

example : https://sourcedcode.com/blog/aem/checking-run-mode-from-sightly-htl-in-aem 



Arun Patidar

Avatar

Community Advisor

Hi @ravindrard 

 

AEM Sightly provides access to the Sling request object through the properties variable. You can leverage the request object to identify the instance based on the context path.

<div data-sly-test="${properties.resourceResolver.descriptor.path == '/content' ? 'author' : 
  properties.resourceResolver.descriptor.path.contains('/content/preview') ? 'preview' : 'publish'}">
  </div>

 

Explanation:

  • properties.resourceResolver.descriptor.path provides the context path of the current request.
  • The code checks if the path starts with /content. This indicates the author instance.
  • It then checks if the path contains /content/preview. This signifies the preview instance.
  • Any other path is assumed to be the publish instance.

 

Or  Leverage Sling Models Exporter API (Optional):

If you're using Sling Models Exporters, you can access the exporter instance within your Sightly component. The exporter might provide properties or methods to identify the instance.

Example (if your exporter has an isAuthor property):

<div data-sly-test="${exportModel.isAuthor ? 'author' : exportModel.isPreview ? 'preview' : 'publish'}">
  </div>

 

The first approach using the Sling request API is the most common and reliable way to identify the instance within Sightly code.

 

Thanks !

 

Avatar

Community Advisor

Based on my understanding, your intention is to implement Sling Dynamic Include (SDI) exclusively on the Publish Instance while keeping the Author Instance unchanged. To achieve this, you can place the org.apache.sling.dynamicinclude.Configuration~aem-demo.cfg.json file only in the config.publish directory. This ensures that SDI will affect only the Publish Instance.

Avatar

Community Advisor

@ravindrard Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Esteban Bustamante