Page 1 of 1

Fallback image if content_image is not set

Posted: Sun Jan 14, 2024 4:57 pm
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

Re: Fallback image if content_image is not set

Posted: Sun Jan 14, 2024 5:03 pm
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}">

Re: Fallback image if content_image is not set

Posted: Sun Jan 14, 2024 6:03 pm
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">

Re: Fallback image if content_image is not set

Posted: Sun Jan 14, 2024 6:08 pm
by DIGI3
Right - I did that before coffee and got the parameter in the wrong spot. Glad you figured it out.