Checking run mode from Sightly HTL in AEM

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.

slingSettingsService is a deprecated service
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>
&nbsp;

<!--/* Staging */-->
<h1>In Staging</h1>
&nbsp;

<!--/* Production */-->
<h1>In Production</h1>
&nbsp;

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;
}
}

Hello, I am an enthusiastic Adobe Community Advisor and a seasoned Lead AEM Developer. I am currently serving as an AEM Technical Lead at MNPDigital.ca, bringing over a decade of extensive web engineering experience and more than eight years of practical AEM experience to the table. My goal is to give back to the AEM Full Stack Development community by sharing my wealth of knowledge with others. You can connect with me on LinkedIn.

2 thoughts on “Checking run mode from Sightly HTL in AEM

Leave a Reply

Your email address will not be published. Required fields are marked *


Back To Top