Page 1 of 1

Change background image from page?

Posted: Sun Sep 24, 2017 3:47 pm
by Korvell
Is there some way I can change the background image of a page directly from the page itself by means of some scripting? For the moment I use several sets of complete alike templates except for the background image, but it is a big hassle to make changes to the layout as I have to change the same thing in each and every template...

Re: Change background image from page?

Posted: Sun Sep 24, 2017 4:05 pm
by cmb
Korvell wrote:
Korvell wrote:Is there some way I can change the background image of a page directly from the page itself by means of some scripting?
You can employ a similar technique to http://www.cmsimple-xh.org/wiki/doku.ph ... der_images. For instance, modify template.htm:

Code: Select all

<?php $backgroundimage = 'some/default/image.ext'?>
<body style="background-image: <?=$backgroundimage?>" <?=onload()?>>
On each page you want to change the default background image, write:

Code: Select all

#CMSimple $backgroundimage = 'image/for/this/page.ext';#

Re: Change background image from page?

Posted: Sun Sep 24, 2017 5:10 pm
by Korvell
Thanks.
But shouldn't the background-image syntax in the body paragraph be like

Code: Select all

url('<?=$backgroundimage?>')"
?

Because I had to change it to that for it to work - well almost work. Problem now is that the variable doesn't get transfered from the page script. Do I recall something about #CMSimple scripting is not working from CMSimple_XH version 1.7 ?

Re: Change background image from page?

Posted: Sun Sep 24, 2017 8:39 pm
by cmb
Indeed, the url() notation required. The remaining issue is caused by the template overwriting the background image defined on the page. The template part should be more like:

Code: Select all

<?php if (!$backgroundimage) $backgroundimage = 'some/default/image.ext'?>
<body style="background-image: url('<?=$backgroundimage?>')" <?=onload()?>>

Re: Change background image from page?

Posted: Sun Sep 24, 2017 9:33 pm
by Korvell
I'm really sorry but it still doesn't work...
The variable from the page scripts simply doesn't transfer to the template - the variable is empty when the call is made in the template. Could it be because the background-image is set within the BODY tag?

Update: Been doing some more testing and no matter where I call the variable $backgroundimage in the template, it is empty unless I set it in the template as well... So it seems the variable is not being set by the #CMSimple script on the page...

Re: Change background image from page?

Posted: Mon Sep 25, 2017 6:29 am
by Tata
I use the trick for a banner. But it equally works with BODY tag as well.
1. template.htm

Code: Select all

    <div id="header" class="banner <?php if($banner == ''){echo 'banner';}else{ echo $banner;}?>">
    If no banner image is specified on a page, standard image is used.
2. stylesheet.css

Code: Select all

.banner{
    background-image: url(images/banner/banner.jpg);
}.banner1{
    background-image: url(images/banner/banner1.jpg);
}
.banner2{
    background-image: url(images/banner/banner2.jpg)  ;
}
.banner3{
    background-image: url(images/banner/banner3.jpg)  ;
} 
3. on pages:

Code: Select all

<h1>Page 1</h1>
<div>#CMSimple $banner="banner1";#</div>
<h1>Page 2</h1>
<div>#CMSimple $banner="banner2";#</div>
<h1>Page 3</h1>
<div>#CMSimple $banner="banner3";#</div>    
Maybe the variable $banner should be defined before its first call in template. But it works fine anyway. At least with v.1.6.10. Not tested with 1.7. so far.

Re: Change background image from page?

Posted: Mon Sep 25, 2017 9:50 am
by cmb
Tata wrote:Maybe the variable $banner should be defined before its first call in template. But it works fine anyway. At least with v.1.6.10. Not tested with 1.7. so far.
Ah, that's it! Indeed, as of CMSimple_XH 1.7.0 the variable would have to be declared upfront (for instance in cmsimple/userfuncs.php), or one has to write:

Code: Select all

#CMSimple $GLOBALS['backgroundimage'] = 'path/to/image.jpg';#
(Note that $GLOBALS['backgroundimage'] and $backgroundimage are basically the same variable.)

Also note that the Morepagedata plugin can also be used for such purposes.

Re: Change background image from page?

Posted: Mon Sep 25, 2017 11:34 am
by lck
Also an Option, see Wiki "Tips and Tricks"

Example: en-Start.css

Code: Select all

#body {
    background: url(images/bg-01.jpg) 50% / cover no-repeat fixed #222;
}
... and so on.

Re: Change background image from page?

Posted: Mon Sep 25, 2017 3:31 pm
by Korvell
Wow! I'm learning so much new today :)
GLOBALS did it - and Morepagedata makes it so convenient and easy for my client to understand and control.

THANK YOU all !

I better get reading and playing with all those tips ;)

Re: Change background image from page?

Posted: Thu Nov 30, 2017 8:50 am
by manu
cmb wrote:
Tata wrote:Maybe the variable $banner should be defined before its first call in template. But it works fine anyway. At least with v.1.6.10. Not tested with 1.7. so far.
Ah, that's it! Indeed, as of CMSimple_XH 1.7.0 the variable would have to be declared upfront (for instance in cmsimple/userfuncs.php), or one has to write:

Code: Select all

#CMSimple $GLOBALS['backgroundimage'] = 'path/to/image.jpg';#
(Note that $GLOBALS['backgroundimage'] and $backgroundimage are basically the same variable.)

Also note that the Morepagedata plugin can also be used for such purposes.
This should go somewhere into the Wiki: Assignment of variables in CMSimple scripting. New behaviour up from 1.7.x.