Hi @ADS,
This is due to the order that Matrix will resolve the keywords vs running the SSJS.
You need to force the keyword to only resolve after the SSJS has run, but splitting the string up and having it only become a proper keyword at the end, prior to the final rendering of the page.
e.g.
tileLink = `%` + `globals_asset_metadata_tileLink:${item}%`,
When Matrix comes across the percentage sign %
, it looks to see if the very next characters are asset
, globals
, frontend
or a handful of other possible keyword prefixes. Here it would be the closing backtick so it ignores it (i.e. treats it just as regular text).
Then the SSJS runs and combines it into an actual keyword, something like %globals_asset_metadata_tileLink:12345%
, after which Matrix does another pass to resolve keywords again, and this time would do the actual replacement to get the value of that metadata field for that asset.
The Server Side JavaScript page describes the processing order of SSJS in more detail. I think you could also try adding evalkeywords="post"
to the <script runat="server">
tag to get a similar effect with your original code, but depending on whether you have other global variables in there, it may affect other SSJS code.
The Create a dynamic asset listing using SSJS tutorial also has an example of splitting the keywords.
Hope that helps!
—iain