Simple edit layout ssjs keywords


(Grant) #1

Hello,

I’m trying to build some simple edit layouts (SEL) to improve our UX when editing/creating content. I’m wondering if theres any way to get the keyword %metadata-F_XXXXXX% printed through ssjs to work in a SEL?

Essentially im trying to loop through a metadata scheme to create a list of cards rather than typing all of the metadata keywords, but when i replace the ID in the keyword it just prints the keywords as a string without processing it.

I’ve tried with a Lodash template, standard page and another SEL. This is what im doing at the moment (lodash template):

<script runat="server">
var panelMetadata = %asset_data%.metadata;
var panelTmpl = _.template("%globals_asset_contents_raw:980234^json_encode%");

function metadataValue(field) {
     return { "id": panelMetadata[field].fieldid, "value": panelMetadata[field].value };
}
</script>

<!--@@ Other html code here @@-->

<script runat="server" (evalkeywords="post" doesn't change anything)> 
// where i want the cards to appear
var assetCount = 6;
            
for (var i = 1; i <= assetCount; i++) {
   var panelPrefix = "panel" + i + ".";
                
   print(panelTmpl({
       "title":        metadataValue(panelPrefix + "title"), 
       "titleLink":    metadataValue(panelPrefix + "title-link"), 
       "image":        metadataValue(panelPrefix + "image"), 
       "text":         metadataValue(panelPrefix + "text"),
       "linkTarget":   metadataValue(panelPrefix + "link-target"), 
       "linkText":     metadataValue(panelPrefix + "link-text")
   }));
}
            
</script>

The template then prints everything out fine except for the %metadata-F_123123%.
I should note i tried other global keywords in the template and they worked fine (globals_date, etc).

I’m assuming SEL’s process the metadata field keywords differently and they are processed before ssjs is?

Is there a way around this or a way this can be achieved?
My squiz version is 5.5.2.2

Thanks for your time! :slight_smile:

Cheers,
Grant


(Bart Banda) #2

Hi Grant,
This won’t work as the %metadata_ keywords have to get evaluated before SSJS is run due to them needing to be asset context aware.

I would maybe try using Matrix’s default metadata output of the metadata fields and then loop through them using client-side JS to alter the DOM or use additional custom CSS to style them in anyway you want.