Sometimes during development, we would need to check the run mode in our AEM environment, so we can show different content based on the context. From the Sightly HTL API, it’s possible to check for the wcmmode, but not the runmode.
In this article, we will set up a sling model helper which will be a sling model, and then use Sightly HTL, data-sly-use, API, to access which run mode we are in. How to check runmode in AEM Sightly HTL can never be this easy.
We understand that the slingSettingsService is a deprecated service, and you would still like to find a way to retrieve the run mode where it equals to “development”, “staging”, or “production”, I wrote a new article that can solve this issue, https://sourcedcode.com/blog/aem/checking-run-mode-from-sightly-htl-in-aem-in-2023. Take a look!
Sightly Code
1 2 3 4 5 6 7 8 9 10 11 | <!--/* Production */--> <h1>In Development</h1> <!--/* Staging */--> <h1>In Staging</h1> <!--/* Production */--> <h1>In Production</h1> |
Java Sling Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.sourcedcode.core.models.utils; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.OSGiService; import org.apache.sling.models.annotations.injectorspecific.RequestAttribute; import org.apache.sling.settings.SlingSettingsService; import javax.annotation.PostConstruct; @Model(adaptables = SlingHttpServletRequest.class) public class RunModeHelper { @OSGiService private SlingSettingsService slingSettingsService; @RequestAttribute @Default(values = "null") private String runmode; private boolean hasRunMode = false; @PostConstruct public void init() { String runModes = slingSettingsService.getRunModes().toString(); hasRunMode = runModes.contains(runmode); } public boolean getHasRunMode() { return hasRunMode; } } |
Sling settings service is deprecated
Hi Abhimanyu, thank you so much for sharing your thoughts on my blog. In response to what you have said, I have created a 2023 version of this blog. It should be a simple replacement of the Sling Service, https://sourcedcode.com/blog/aem/checking-run-mode-from-sightly-htl-in-aem-in-2023