Page 1 of 1
Default value for page_attr
Posted: Sat Aug 22, 2020 2:50 pm
by caigner
I am using the page attribute 'alias' to switch background images like so:
Code: Select all
</__body style="background-image: url(https://www.my-domain.com/uploads/images/background/bg_{page_attr key='alias'}.jpg);">
Now, if that background image does not exist I would like to use a generic
bg_default.jpg. How can this be accomplished?
Re: Default value for page_attr
Posted: Sat Aug 22, 2020 3:43 pm
by caigner
By some trial and error I found this to work for me:
Code: Select all
{page_attr key='alias' assign='bgImage'}
{$imageName="bg_default.jpg"}
{if !file_exists("/uploads/images/background/bg_$bgImage.jpg")}
{$imageName="bg_{$bgImage}.jpg"}
{/if}
</__body style="background-image: url(https://www.my-domain.com/uploads/images/background/{$imageName});">
It works, but I can't explain, why I have to use
!file_exists.
Re: Default value for page_attr
Posted: Sat Aug 22, 2020 3:57 pm
by caigner
Well, my joy about the solution didn't last long.
Actually it doesn't work as I expected. So I am still looking for an answer.
Re: Default value for page_attr
Posted: Sat Aug 22, 2020 4:02 pm
by caigner
Oh, now it works. Take note of the path in the if clause: The first / was too much.
Code: Select all
{page_attr key='alias' assign='bgImage'}
{$imageName="bg_default.jpg"}
{if file_exists("uploads/images/background/bg_$bgImage.jpg")}
{$imageName="bg_{$bgImage}.jpg"}
{/if}
</__body style="background-image: url(https://www.my-domain.com/uploads/images/background/{$imageName});">
Re: Default value for page_attr
Posted: Sat Aug 22, 2020 4:07 pm
by DIGI3
I think you're on the right track, but need to do some troubleshooting. I think file_exists will need the full server path, not just from the web root (like /home/domain/public_html/uploads/images/etc). It might also be better to build the path as a variable first, then test it. Something like:
{$path="/path/to/$myfile"}
{if file_exists($path)}...{/if}