MODX Community

How do I test if the image field has a value?

I have this line to display a product image:

<p><img src="[[commerce.get_product? &product=`[[+tv.products]]` &field=`image`]]" /></p>

I’ve tried using placeholders to show this image to no avail.
How do I not display this line if the image field has no value – no image code text??
I can not use ‘notempty’ because this particular item will not display using placeholders.
Thank you, Tom

Your example line is missing from your post.
Maybe you can use the notempty modifier?

See: https://docs.modx.com/current/en/building-sites/tag-syntax/output-filters#string-output-modifiers

Also see this for the best way to build your conditional tags:

1 Like

I fixed the line code, thank you for catching that.
I must test on this exact line of code only, not anything else…alternate codings to present the image have not worked for me…Idk why, I am not a MODx expert.
Thank you, Tom

Maybe one of these solutions works.

  • Using a placeholder:
[[commerce.get_product? &product=`[[+tv.products]]` &toPlaceholders=`product`]]
[[+product.image:notempty=`<p><img src="[[+product.image]]" /></p>`]]
  • or using an additional chunk:
[[commerce.get_product? &product=`[[+tv.products]]` &tpl=`myImageTemplate`]]

with the chunk myImageTemplate

[[+image:notempty=`<p><img src="[[+image]]" /></p>`]]

I’d use this:

<p><img src="[[commerce.get_product:default=`/some/default/image.jpg`? &product=`[[+tv.products]]` &field=`image`]]" /></p>

I must test on this exact line of code only, not anything else…

Here is another possible solution that uses the output modifier toPlaceholder.

[[commerce.get_product:toPlaceholder=`myImg`? &product=`[[+tv.products]]` &field=`image`]]
[[+myImg:notempty=`<p><img src="[[+myImg]]" /></p>`]]
1 Like

The code from @markh works though I did it as

<p><img src="[[commerce.get_product:default=``? &product=`[[+tv.products]]` &field=`image`]]" />`</p>

but then I realized I need MODx to not write the paragraph tag if the image is blank…more research will be done but if anyone can point me to something that would be nice
I tried

[[[[commerce.get_product:default=``? <p>&product=`[[+tv.products]]` &field=`image`]]</p>]]

but it doesn’t do anything
AAAAAAAAAAAAAND @halftrainedharry’s suggestion is the winning code!!
This is now marked as the desired/wanted answer.
It does what’s needed – get the image and not write the <p> tag if the image does not exist. :slight_smile: :slight_smile:
Thank you everyone!! :slight_smile: tom

1 Like