Fallback image if content_image is not set

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
User avatar
caigner
Forum Members
Forum Members
Posts: 46
Joined: Sun Aug 16, 2020 5:29 am

Fallback image if content_image is not set

Post by caigner »

In case the user does not provide a content_image I want to display a default (fallback) image. So I wrote these lines in my template:

Code: Select all

{if isset(content_image)}
   {content_image dir="images/headers/" block='Header-Bild'}
{else}
   <img src="/uploads/images/headers/header_default_o.jpg">
{/if}
This displays the content_image if it is set, but fails to display the fallback image.

I then tried content_image with a $ sign:

Code: Select all

{if isset($content_image)}
   {content_image dir="images/headers/" block='Header-Bild'}
{else}
   <img src="/uploads/images/headers/header_default_o.jpg">
{/if}
Now only the fallback image is displayed.

What am I missing?

Cheers,
Christian
I am using CMSms 2.2.19
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1629
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Fallback image if content_image is not set

Post by DIGI3 »

Because {content_image} is a tag and not a variable, it doesn't quite work the same. Try something like this:

Code: Select all

{content_image assign='content_image'}
<img src="{$content_image|default:'/uploads/images/headers/header_default_o.jpg' urlonly=1}">
Not getting the answer you need? CMSMS support options
User avatar
caigner
Forum Members
Forum Members
Posts: 46
Joined: Sun Aug 16, 2020 5:29 am

Re: Fallback image if content_image is not set

Post by caigner »

Thanks for pointing me into the right direction. I tweaked your suggestion a little bit and now it works fine:

Code: Select all

{content_image block='Header-Foto' assign='myContentImage' dir='images/headers' urlonly=1}

<img class="hero" src="{$myContentImage|default:'/uploads/images/headers/header_default_o.jpg'}" title="Header-Foto" alt="Header-Foto">
I am using CMSms 2.2.19
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1629
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Fallback image if content_image is not set

Post by DIGI3 »

Right - I did that before coffee and got the parameter in the wrong spot. Glad you figured it out.
Not getting the answer you need? CMSMS support options
Post Reply

Return to “CMSMS Core”