IF metadata exists THEN


(Bdebroglio) #1

Hi,

 

I have a number of metadata fields that hold contact details (telephone, email, Facebook, etc). I would like to print an unordered list - but print only metadata items that exist. So, if no Facebook, then don't output a LI item with a Facebook link.

 

I found the keyword modifier, but this doesn't work, presumably because there is HTML within the modifier?

%asset_metadata_USER.facebook^notempty:<li><a href="{asset_metadata_USER.facebook}">Facebook</a></li>%

(Talk) #2

Hi bern,

 

The cleanest way would be to use a Paint Layout for your assets. Create a paint layout, and then create new conditional keywords on its type format's conditional keywords screen.

 

Your facebook condition could be something like:

%asset_metadata_USER.facebook%

Does not match

^$

Where ^$ is a regular expression for empty.

 

You can then wrap your listed items in the conditional keywords like so:

%begin_facebook%
%asset_metadata_USER.facebook^notempty:<li><a href="{asset_metadata_USER.facebook}">Facebook</a></li>%
%end_facebook%

(Tim Davison) #3

I'm at home so I don't have anything to test this with, but I think you're missing the ^replace_keywords modifier (this tells it to replace what's in the '{}').  So you'd need something like:

%asset_metadata_USER.facebook^replace_keywords:notempty:<li><a href="{asset_metadata_USER.facebook}">Facebook</a></li>%

Matrix keywords don't have a very elegant if/else logic system, at least not in-page, so you end up doing some whacky stuff to get it to do what you want.  What option to take depends a lot on the context.

 

Another Option:

Sometimes you can use the tagif:<tagname> modifier.  This wraps the value in a tag only if there is a value.  Useful if you can get away with, for example, a plain list item, but you're doing something more complex.  Useful sometimes.

 

Another Option:

You could also do something like:

<li class="%asset_metadata_USER.facebook^charcount^eq:0:hidden: ^trim%">
  <a href="%asset_metadata_USER.facebook%">Facebook</a>
</li>

And just make sure you have a hidden class (or similar) that will hide this item (e.g. .hidden { display: none !important; } ).

 

P.S. the space on the end is required, otherwise system will print a '0' (in other words a 'false') for cases where there is a value.  You then add the ^trim on the end to remove the space again.  I always do 'cause I'm a neat freak.

 

Another Option:

CSS-only solution.

HTML:
<a href="%asset_metadata_USER.facebook%">Facebook</a>

CSS:
a[href=""] { display: none; }

You'd probably want to control it a little more with ancestor classes so it only targets a very specific part of your page, and you wouldn't be able to hide the parent li item, so probably not so useful in your case.

 

Another Option:

Combine HTML comments with the keywords (this is my least favourite approach, but have at times been forced to use it).

<!-- %asset_metadata_USER.facebook^notempty: --> %
<li><a href="%asset_metadata_USER.facebook%">Facebook</a></li>
%asset_metadata_USER.facebook^notempty: <!-- % -->

I don't even want to explain that one - it's late.  Basically, the 'outer' comments always print.  If there is a value, the 'inner' comments also print, and essentially cancel out the outer comments.  If the value is empty, on the other hand, the inner comments do not print, so the entire thing gets rendered as just one comment.  Like below:

Rendered HTML if empty (all commented):
<!-- <li><a href="">Facebook</a></li> -->

Rendered HTML if has value (comments cancel each other):
<!-- --> <li><a href=“your-value”>Facebook</a></li> <!-- --> 

This approach is useful to show/hide larger chunks of page that might have a lot of other keywords in them, and where a Paint Layout is just too heavy or not an option.

 

Hope that all makes sense!!


(Nic Hubbard) #4

Alternately, you could just use the :empty css selector to hide the empty list items.


(Bdebroglio) #5

Thanks all.

 

I had omitted the ^replace_keywords modifier.

 

But the other suggestions are helpful. Appreciated.