Hi Team,
I have a requirement, I have to show a different piece of HTML if it's dev, and if it's prod I have to show other HTML.
how we can achieve this in sightly or in sling model.
Solved! Go to Solution.
Views
Replies
Total Likes
@raushan123 You can create an AEM run mode OSGI service which will have the ability to return us to either “development”, “staging”, or “production”. Basically, you would need to create a RunModeIndentifierService and create configuration files & place them under the run mode configuration folder (config.dev, config.prod). Moreover, in the Sling Model with the help of @OSGIService annotation, you can call this service and get the actual run mode. Finally, in the sightly, you can provide a condition to display the HTML based on the run mode value coming from Sling Model.
Detailed step on this blog: https://sourcedcode.com/blog/aem/checking-run-mode-from-sightly-htl-in-aem-in-2023
Regards,
Ayush
Hello @raushan123
You can combine runmodes with HTL templates to Achieve the same.
Example:
- Get Runmode from Sling Model
https://sourcedcode.com/blog/aem/checking-run-mode-from-sightly-htl-in-aem
- Check for runmode and then call the relevent HTL template
https://medium.com/@toimrank/aem-data-sly-template-and-data-sly-call-usage-2999ca8225b6
@raushan123 You can create an AEM run mode OSGI service which will have the ability to return us to either “development”, “staging”, or “production”. Basically, you would need to create a RunModeIndentifierService and create configuration files & place them under the run mode configuration folder (config.dev, config.prod). Moreover, in the Sling Model with the help of @OSGIService annotation, you can call this service and get the actual run mode. Finally, in the sightly, you can provide a condition to display the HTML based on the run mode value coming from Sling Model.
Detailed step on this blog: https://sourcedcode.com/blog/aem/checking-run-mode-from-sightly-htl-in-aem-in-2023
Regards,
Ayush
Hello @raushan123 -
In AEM as a Cloud Service, direct access to runmodes is not available. Runmodes are primarily used in AEM on-premise installations to define specific configurations and behavior for different environments.
However, Adobe provides an alternative approach for managing environment-specific configurations in AEM as a Cloud Service. The recommended approach is to use Cloud Manager and the Configuration Management feature to handle environment-specific settings.
Here's how to set up environment-specific configurations using Configuration Management in AEM as a Cloud Service:
1. Access Cloud Manager: Log in to Adobe Experience Cloud and navigate to the Cloud Manager console.
2. Configure Configuration Management: In the Pipeline configuration, enable the Configuration Management feature. This allows you to manage environment-specific configurations for your AEM project.
3. Define Configuration Properties: Within the Configuration Management section, define the properties or settings that you want to manage for your AEM project. These properties can be environment-specific, such as database connections, API endpoints, or any other configurations that differ between environments.
4. Create Configuration Sets: Create different configuration sets for each environment, such as Dev, Stage, and Prod. Within each configuration set, you can specify the values for the defined properties.
5. Assign Configuration Sets to Environments: Assign the appropriate configuration set to each environment in your Pipeline. For example, assign the Dev configuration set to the Dev environment, the Stage configuration set to the Stage environment, and so on.
6. Deploy Configuration Sets: When you deploy your AEM project using Cloud Manager, the associated configuration set for each environment will be deployed along with the code. This ensures that the correct configurations are applied to each environment during the deployment process.
Now, once this is done you can retrieve the environment-specific configurations using the ConfigurationManager service in a sling model as below :
import com.adobe.granite.confmgr.Configuration;
import com.adobe.granite.confmgr.ConfigurationManager;
import com.adobe.granite.confmgr.ConfigurationManagerFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@Model(adaptables = SlingHttpServletRequest.class)
public class MyModel {
@Inject
private ConfigurationManager configManager;
public String getEnvironmentType() {
Configuration envConfig = configManager.getConfiguration("env-configuration-set");
return envConfig.getProperties().get("envType.property", String.class);
}
}
And later can leverage the same in your HTL code as follows :
<div data-sly-use.model="com.example.MyModel">
<sly data-sly-test="${model.environmentType == 'dev'}">
<!-- Render dev-specific code -->
</sly>
<sly data-sly-test="${model.environmentType == 'prod'}">
<!-- Render prod-specific code -->
</sly>
</div>
Hello @raushan123 -
Additionally, if you think setting up environment-specific configurations using Configuration Management in AEM is a bit of a task. Here is one more alternative solution to identify environment type.
@Model(adaptables = SlingHttpServletRequest.class)
public class MyModel {
@Inject
private SlingHttpServletRequest request;
public String getEnvironmentType() {
String hostname = request.getServerName();
if (hostname.contains("dev")) {
return "dev";
} else if (hostname.contains("stage")) {
return "stage";
} else if (hostname.contains("prod")) {
return "prod";
} else {
return "unknown";
}
}
}
But i would still say if it possible then go with setting up environment-specific configurations to set up environment type.