Change background image from page?

About the template and stylesheet - and changing the menu
Korvell
Posts: 93
Joined: Thu May 22, 2008 10:33 pm

Change background image from page?

Postby Korvell » Sun Sep 24, 2017 3:47 pm

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...

cmb
Posts: 12051
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

Re: Change background image from page?

Postby cmb » Sun Sep 24, 2017 4:05 pm

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';#
Christoph M. Becker –Plugins for CMSimple_XH, but not for CMSimple 4+

Korvell
Posts: 93
Joined: Thu May 22, 2008 10:33 pm

Re: Change background image from page?

Postby Korvell » Sun Sep 24, 2017 5:10 pm

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 ?

cmb
Posts: 12051
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

Re: Change background image from page?

Postby cmb » Sun Sep 24, 2017 8:39 pm

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()?>>
Christoph M. Becker –Plugins for CMSimple_XH, but not for CMSimple 4+

Korvell
Posts: 93
Joined: Thu May 22, 2008 10:33 pm

Re: Change background image from page?

Postby Korvell » Sun Sep 24, 2017 9:33 pm

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...

Tata
Posts: 2531
Joined: Tue May 20, 2008 5:34 am
Location: Slovakia
Contact:

Re: Change background image from page?

Postby Tata » Mon Sep 25, 2017 6:29 am

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-imageurl(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.
CMSimple.sk
It's no shame to ask for an answer if all efforts failed.
But it's awful to ask without any effort to find the answer yourself.

cmb
Posts: 12051
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

Re: Change background image from page?

Postby cmb » Mon Sep 25, 2017 9:50 am

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.
Christoph M. Becker –Plugins for CMSimple_XH, but not for CMSimple 4+

lck
Posts: 1031
Joined: Wed Mar 23, 2011 11:43 am
Location: Dahoam ;)
Contact:

Re: Change background image from page?

Postby lck » Mon Sep 25, 2017 11:34 am

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.
Bevor du den Pfeil der Wahrheit abschießt, tauche die Spitze in Honig ;)       ► CMSimple_XH Templates & OnePage-Templates ◄

Korvell
Posts: 93
Joined: Thu May 22, 2008 10:33 pm

Re: Change background image from page?

Postby Korvell » Mon Sep 25, 2017 3:31 pm

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 ;)

manu
Posts: 618
Joined: Wed Jun 04, 2008 12:05 pm
Location: St. Gallen - Schweiz
Contact:

Re: Change background image from page?

Postby manu » Thu Nov 30, 2017 8:50 am

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.


Return to “Changing layout”

Who is online

Users browsing this forum: No registered users and 2 guests