jcu-eresearch / matrix-helpers

100% Unguessable™. Public bug tracker, pages, designs and helpers that make life in Squiz Matrix easier. The metaphorical red pill.
https://jcu-eresearch.github.io/matrix-helpers/
GNU Affero General Public License v3.0
2 stars 0 forks source link

Conditional Keywords output by SSJS don't work and are ignored, resulting in inner content being included #76

Open PaulineLawrey opened 7 years ago

PaulineLawrey commented 7 years ago

In a Content Container Template Paint Layout, I tried using conditional keywords in my SSJS output. However, although the conditional keywords were removed from the final output, the content inside the conditional was still included, even though the condition was false. This behaviour is similar to how Conditional Keywords in a Design parse file weren't evaluated; at least before that feature was implemented via https://squizmap.squiz.net/matrix/9732.

Here's an simplified example:

 Asset with no image - using SSJS
<script runat="server">
  print('%begin_asset_thumbnail_url' + '%')
  print('<pre>This should not get output: asset image url "%asset_thumbnail_url' + '%"</pre>')
  print('%else_asset' + `'%')`
  print('<pre>No image found</pre>')
  print('%end_asset' + '%')
</script>

 Asset with no image - not using SSJS

%begin_asset_thumbnail_url%
  <pre>This should not get output: asset image url '%asset_thumbnail_url%'</pre>
%else_asset%
  <pre>No image found</pre>
%end_asset%

Gives an output of:

Asset with no image - using SSJS
This should not get output: asset image url ""
No image found

Asset with no image - not using SSJS
No image found

noting the conditional being printed out of SSJS is being ignored.

For note, the reason that a SSJS if statement can't be used in this instance is because this Paint Layout's SSJS is being used to generate keywords that get processed in the output. The Content Container Template has a metadata schema of related items, which is can be evaluated on starting the SSJS block, but that list of related items can be an arbitrary length and various keywords need to be evaluated on each of the assets in the list. So, in the Paint Layout, I can use a forEach statement to iterate over the list of IDs, but can't get evaluate their keywords.

This issue would be resolved if a SSJS API was available (eg see https://squizmap.squiz.net/matrix/10541), resolving the messy state of getting data in or out of SSJS.

davidjb commented 7 years ago

Reported at https://squizmap.squiz.net/matrix/10711.

davidjb commented 7 years ago

Response is that conditional keywords are only supported in the assets that generates PHP content file at the compile time (Bodycopy Container/Design). Essentially, conditionals get converted into if statements in PHP, which doesn't fly in this context because keyword replacements are string-parsing only.

I doubt we'll see a resolution to this one; the code base which powers all these replacement layers is already spaghetti. So, let's hope for #55 instead and we can do away with keywords entirely.