How to run a specific HTL Snippet in Dev and Stage Instance only and not in prod | Community
Skip to main content
Nitesh-Chavan
Level 3
July 7, 2025
Solved

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

  • July 7, 2025
  • 2 replies
  • 624 views

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.  


2 replies

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
July 7, 2025
Nitesh-Chavan
Level 3
July 7, 2025

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. 

Level 4
July 7, 2025

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.

Nitesh-Chavan
Level 3
July 7, 2025

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

Thank You.