Skip to content

Instantly share code, notes, and snippets.

@gabrielwalt
Last active May 17, 2021 14:09
Show Gist options
  • Save gabrielwalt/278f8cee870aac7ec619 to your computer and use it in GitHub Desktop.
Save gabrielwalt/278f8cee870aac7ec619 to your computer and use it in GitHub Desktop.
Read AEM runmodes from Sightly
var SlingSettingsService = Packages.org.apache.sling.settings.SlingSettingsService;
use(function () {
// Get runmodes and transform them into an object that is easier to read for Sightly
var runmodesObj = {};
var runmodesSet = sling.getService(SlingSettingsService).getRunModes();
var iterator = runmodesSet.iterator();
while (iterator.hasNext()) {
runmodesObj[iterator.next()] = true;
}
return {
runmodes: runmodesObj
}
});
<div data-sly-use.logic="logic.js">
<p>Current runmodes: ${logic.runmodes}</p>
<p data-sly-test="${logic.runmodes.samplecontent}">samplecontent runmode</p>
</div>
@paulrohrbeck
Copy link

@gabrielwalt If code should not act on the run mode, what's your suggestion for handling a scenario where different code is needed between a stage and prod environment? Adding an OSGi config just to distinguish the two and then using that in Sightly seems a bit overkill.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment