Creating Global Variables


#1

Matrix Version: 5.4.1

On this manuals page under the heading “Creating Global Variables” …

<script runat="server">
//Global variables
var _siteSection = '%frontend_asset_url_path^explode:/^index:1^empty:home%'; 
var _pageType    = '%frontend_asset_assetid^replace:{globals_site_assetid}|{globals_site_index_id}:H^eq:H:home:inside%';
var _pageLevel   = %frontend_asset_url_path^explode:/^count^subtract:1%;
var _titleTag    = '%frontend_asset_name^escapequotes% | %globals_site_name%';
</script>

Can we use the variables created here (like _siteSection) on some other page ? Are these really Global variables ? The heading is confusing me ?

What I am trying to do is pass an “id” to construct an endpoint in REST Resource asset ? The “id” is generated in a “server side JS code”.


(Douglas (@finnatic at @waikato)) #2

They’re global variables within the scope of the script on any pages it gets included on AFAIK.


#3

Thanks Douglas, That’s what I am thinking. So essentially they can’t be used like Global Keywords.


(Bart Banda) #4

They can be used as global variables within SSJS. Global keywords are not SSJS and are evaluated separately.

However, you could create a Standard Page, that inside it has some SSJS printing out a value, lets say for example it has this:

<script runat="server">print('abc')</script>

Then reference the contents of that Standard Page using a keyword like this: %globals_asset_contents_raw:1234^run_ssjs%, which would give you:

abc