Detecting presence of image variety not working


(Andrew Harris) #1

Matrix Version: v5.4.1.3

I’m dynamically viewing an image asset based on an ID in a get variable, using a paint layout, eg: %globals_get_banner^as_asset:asset_contents_paint_2692687%
most keywords are working fine in the paint layout, eg %image_width% and %image_v_medium_width%.

I need to detect the presence of a particular variety, lets say ‘large’. So, I tried this:
%begin_image_v_large% = display the large variety %else_image% = display a warning %end_image%

The result is always “= display the large variety”, regardless of whether a large version exists, or not.

I’ve tried a number of ever more desperate variations on this theme:
%begin_image_v_large_url^contains:h%
and so, on, to no avail.

What am I missing here?


(Nic Hubbard) #2

I use the following and it works:

%begin_image_v_2x%
  <img src="%image_v_2x_url%" class="is-block" alt="%asset_name%" />
%else_assset%
    <img src="%asset_url%" class="is-block" alt="%asset_name%" />
%end_assset%

I think your issue is your incorrect end tag.


(Bart Banda) #3

The end tag seems ok, but I’m wondering what just %image_v_large% prints by itself when there is no larger version? Might be a bug with the begin/else/end keyword logic for image keywords in that version.

Another version to try is something like this:

%begin_image_v_large^charcount^gt:1:true:%

You can actually also try and see what %image_v_large^charcount% gives you for the ones that are supposed to be empty.


(Andrew Harris) #4

Bart, I’m going with the bug scenario!

To clarify, I tried some keywords based on your suggestion:
image_v_large^charcount^gt:1:true: = %image_v_large^charcount^gt:1:true:%

%begin_image_v_large^charcount^gt:1:true:%
= found a large variety
%end_image%


On an asset with no large variety, (but a small variety) the result is:
begin_image_v_large^charcount^gt:1:true: =

= found a large variety


On an asset with a large variety, the result is:
begin_image_v_large^charcount^gt:1:true: = true

= found a large variety


Further digging found that when using the begin/end structure, any variety will do.
So on an asset with no varieties at all, the code only returns:
image_v_large^charcount^gt:1:true: =
The begin structure seems to fail appropriately.


BUT it gets even weirder!

I manually added a ‘small’ variety to that asset, and then removed it, but the keyword still seemed to find something and returned:
image_v_large^charcount^gt:1:true: =

= found a large variety


FWIW, this now has a ticket in the system, SDVIC-768, but the way it’s looking to me, it’s not going to have a quick fix / happy ending.


(Bart Banda) #5

No worries, let us know how the ticket ends up.
As a workaround, there is always SSJS?


(Andrew Harris) #6

hmm - ok, I’ve recently built my first bit of useful SSJS, so I guess that’s an option.

Really like to keep my code using one or the other - matrix keyword conditions OR js - not so keen on mixing them up, purely from a maintenance and debugging point of view. It can make it hard for people later on to work out what’s happening.


(Andrew Harris) #7

OK, we have a resolution, but I definitely think this is a bug.

In certain situations, like this one where I called a Asset Contents with a Paint Layout: %globals_get_banner^as_asset:asset_contents_paint_2692687%
Matrix is unable to use the usual begin/end constructs in the Paint Layout to detect the absence of an image variety, instead, returning a false positive.

So, my original construct fails where a large variety is not present. It still tries to display something.

%begin_image_v_large%
= display the large variety
%else_image%
= display a warning
%end_image%

The workaround was to reverse the postive/negative order, by using a regex asset to test for emptiness. This made a negative result from the regex, a postive result in the begin/end construct.

%begin_image_v_large^preg_match:2732966%
= display a warning
%else_image%
= display the large variety
%end_image%

Messy, but we got there!