Referencing a global variable in any component | Community
Skip to main content
New Participant
Jan 30, 2019 4:18 PM UTC
Solved

Referencing a global variable in any component

I have a figure which is regularly used throughout the website and every time this figure changes, we have to update every page that it is being used on. I was wondering if there was a way to have some sort of global variable where this can referenced in any component using something like "we have [variable] customers" but the resulting text would be "we have 240 customers".

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Gaurav-Behl
Gaurav-BehlAccepted solution
Level 10
Jan 30, 2019 8:56 PM UTC

You could use page session [1] or save it on server side using jcr-node/property/osgi config/sling feature/ or client side using localStorage/sessionStorage/cookie based on your use case.

[1]

Refer - global.jsp <%@page session="true"%>

2 replies

Gaurav-Behl
Gaurav-BehlAccepted solution
Level 10
Jan 30, 2019 8:56 PM UTC

You could use page session [1] or save it on server side using jcr-node/property/osgi config/sling feature/ or client side using localStorage/sessionStorage/cookie based on your use case.

[1]

Refer - global.jsp <%@page session="true"%>

Translate
smacdonald2008
Level 10
Jan 30, 2019 4:24 PM UTC

AEM does not really have "global variable" functionality. Variable scope is defined at the component level - be it Java as the case of Java Sling Models or HTL code. That is, a variable defined in 1 HTL component is not defined in another component.

If you want a value to be applicable across multiple components - I would look at storing a value in an AEM Node. THen in your components - you can code it to read that node value. If you change that node value - it will reflect across multiple components. 

Translate