Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards.
SOLVED

How to run a specific HTL Snippet in Dev and Stage Instance only and not in prod

Avatar

Level 3

Hello Community Members, 

Good Day!

I have a use case, I have a HTL file (present in the Dev, Stage and Prod instances) in which I have a particular HTL Snippet I want to get executed in AEM Stage and Dev instances only (Including Author and Publisher) and strictly not on the Production instance. 

Is there a way to code this logic in HTL so that the HTL Snippet will only gets executed on the Dev and Stage instance. 

Thank You.  


Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
6 Replies

Avatar

Correct answer by
Community Advisor

Avatar

Level 3

Hi @arunpatidar 

Thanks for sharing the above piece of information, however we can see the "slingSettingsService" is deprecated. 

As mentioned by the Author of the post you have shared I will try the new method suggested by the author. 

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

Thank You. 

Avatar

Level 5

Hi @Nitesh-Chavan ,

Yes, you can control execution of HTL code based on the AEM environment (Dev, Stage, Prod) by making use of run modes and exposing them via a Sling Model or Use-API class to your HTL.

 

public class EnvironmentCheck extends WCMUsePojo {

    private boolean isDevOrStage;

    @Override
    public void activate() {
        SlingSettingsService slingSettings = getSlingScriptHelper().getService(SlingSettingsService.class);
        Set<String> runModes = slingSettings.getRunModes();
        isDevOrStage = runModes.contains("dev") || runModes.contains("stage");
    }

    public boolean isDevOrStage() {
        return isDevOrStage;
    }
}

 

 

<data-sly-use.envCheck="com.example.core.utils.EnvironmentCheck" />

<sly data-sly-test="${envCheck.devOrStage}">
  <p>This is shown in Dev/Stage only.</p>
</sly>

 

  • SlingSettingsService.getRunModes() checks current AEM run modes.
  • You expose a flag isDevOrStage to HTL.
  • HTL renders the block only if the run mode is dev or stage.

Set run modes like this in your instance: -Dsling.run.modes=env

Try this and let me know if it works.

Avatar

Level 3

I will check this and I will get back to you on it.

Thank You. 

Avatar

Level 3

Hi @ShivamKumar , 

Thanks for sharing the above piece of information, however we can see the "slingSettingsService" is deprecated. 

 I am will try with the following option.

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

Thank You. 

Avatar

Level 5

Thanks for the information - I wasn’t aware it was deprecated. I’ll check that.