Default value for page_attr

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: 52
Joined: Sun Aug 16, 2020 5:29 am

Default value for page_attr

Post 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?
I am using CMSms 2.2.19
User avatar
caigner
Forum Members
Forum Members
Posts: 52
Joined: Sun Aug 16, 2020 5:29 am

Re: Default value for page_attr

Post 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.
Last edited by caigner on Sat Aug 22, 2020 4:02 pm, edited 1 time in total.
I am using CMSms 2.2.19
User avatar
caigner
Forum Members
Forum Members
Posts: 52
Joined: Sun Aug 16, 2020 5:29 am

Re: Default value for page_attr

Post 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.
I am using CMSms 2.2.19
User avatar
caigner
Forum Members
Forum Members
Posts: 52
Joined: Sun Aug 16, 2020 5:29 am

Re: Default value for page_attr

Post 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});">
I am using CMSms 2.2.19
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1800
Joined: Wed Feb 25, 2009 4:25 am

Re: Default value for page_attr

Post 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}
Not getting the answer you need? CMSMS support options
Post Reply

Return to “CMSMS Core”