Page 1 of 2

Different backgrounds for main menu items

Posted: Fri Nov 09, 2007 3:29 pm
by hadion
Hello all,

I searched the forum, but could not find my solution.

How can I set a background (in the CSS file) to a specific section of the website ( page with childeren) without making a new template with css files?
So not with a tag in the template!

Example:
Page 1 - background1.gif
- Subpage - background1.gif
- Subpage 2 - background1.gif
Page 2 - background2.gif
Page 3 - background3.gif
Page 4 - background3.gif
Page 5 - background3.gif

Thanks in advance,

Hadion

Re: Different backgrounds for main menu items

Posted: Wed Nov 14, 2007 7:53 pm
by neophron
Hi,

go to extensions --> user defined tags. There add a new tag with name page_class. Insert this code:

Code: Select all

global $gCms;
$classes = array();
$manager =& $gCms->GetHierarchyManager();
$thisPage = $gCms->variables['content_id'];
$currentNode = &$manager->sureGetNodeById($thisPage);
while( isset($currentNode) && $currentNode->getLevel() >= 0 )
{
    $currentContent =& $currentNode->getContent();
    array_unshift($classes, $currentContent->Alias());
    $currentNode =& $currentNode->getParentNode();
}
echo implode($classes, ' ') . ' ' . implode($classes, '-') . ' ' . 'page-' . $thisPage;
Save the new tag. Go to your template and make this:
Thats all. Now every page will have a personal body class.

neophron

Re: Different backgrounds for main menu items

Posted: Thu Nov 15, 2007 9:40 pm
by streever
hey,

i think it's even easier if you just do this, in the Template:



now, every body id is equal to the pagealias plus "Page"--so, home is, "homePage

EDIT: I TYPOED.

it's $page_alias, NOT $pagealias!! You need the underline.

Re: Different backgrounds for main menu items

Posted: Sun Nov 18, 2007 2:15 pm
by hadion
thanks all  ;D applaud

Re: Different backgrounds for main menu items

Posted: Thu Nov 22, 2007 7:42 pm
by faglork
neophron wrote:

Code: Select all

global $gCms;
$classes = array();
$manager =& $gCms->GetHierarchyManager();
$thisPage = $gCms->variables['content_id'];
$currentNode = &$manager->sureGetNodeById($thisPage);
while( isset($currentNode) && $currentNode->getLevel() >= 0 )
{
    $currentContent =& $currentNode->getContent();
    array_unshift($classes, $currentContent->Alias());
    $currentNode =& $currentNode->getParentNode();
}
echo implode($classes, ' ') . ' ' . implode($classes, '-') . ' ' . 'page-' . $thisPage;
Save the new tag. Go to your template and make this:
Thats all. Now every page will have a personal body class.

neophron
Yeah, but he wanted a class indicating just the TOP LEVEL categories. So you have to change the last line to

Code: Select all

echo $classes[0];
hth,
Alex

Re: Different backgrounds for main menu items

Posted: Thu Nov 22, 2007 8:10 pm
by faglork
Hi,

just fooled around a bit with this ... found a nice solution.

The UDT (I called it "bgimage") now checks if a file with the corresponding name really  exists, if not it just uses a default pic.

The UDT:

Code: Select all

global $gCms;
$classes = array();
$manager =& $gCms->GetHierarchyManager();
$thisPage = $gCms->variables['content_id'];
$currentNode = &$manager->sureGetNodeById($thisPage);
while( isset($currentNode) && $currentNode->getLevel() >= 0 )
{
    $currentContent =& $currentNode->getContent();
    array_unshift($classes, $currentContent->Alias());
    $currentNode =& $currentNode->getParentNode();
}

$picname =$classes[0];
$filename = '/server/path/to/your/uploads/images/backgrounds/'.$picname.'.jpg';
if (file_exists($filename)) {
    echo '/uploads/images/backgrounds/'.$picname.'.jpg';
} else {
    echo '/uploads/images/backgrounds/standard.jpg';
}
Now, in your page template in the HEADER DIV you use

Code: Select all

<h1><a style="background-image: url({bgimage};" href="/">Your Title goes here</a></h1>
Voila ... now you have a default pic to start with (or just leave it for certain sections), and all you have to do to activate a section-specific background is to upload a file with the same name as the top level  page-alias.

Have fun,
Alex

Re: Different backgrounds for main menu items

Posted: Fri Nov 23, 2007 10:16 am
by lollipop27
Hi,

I love this tag, because I didn't know you can check if files exist...

Is there a possibility to check if the 4th level node page-alias exists and then insert that one, otherwise check if the third level node page-alias exists, then take this one or if non of this works take a default picture?


Thanks a lot....

Re: Different backgrounds for main menu items

Posted: Fri Nov 23, 2007 10:48 am
by faglork
lollipop27 wrote: Is there a possibility to check if the 4th level node page-alias exists and then insert that one, otherwise check if the third level node page-alias exists, then take this one or if non of this works take a default picture?
Yes. The whole list is in the "classes" array ... just find out the length of the array, and then check for
classes[xx] down to classes[0] ...

You could even skip that array altogether. You could check for the file right there in the while loop which generates the array.

But it is okay with the array ... gives you more possibilities.

hth,
Alex

Re: Different backgrounds for main menu items

Posted: Fri Nov 23, 2007 11:19 am
by lollipop27
thanks... great!!!

It works....

Larissa

Re: Different backgrounds for main menu items

Posted: Wed Nov 28, 2007 3:07 pm
by streever
oops

I actually wrote a plugin called "sectiontitle" that can give you an up level title.

check it in dev.cmsmadesimple.org

Re: Different backgrounds for main menu items

Posted: Fri Dec 07, 2007 5:19 am
by declanreynolds
Hi,

I tried this out and it finds the default image but it doesn't find an image with the same name as an alias.

if the alias is 'aboutus' then do I simply call the image 'aboutus.jpg'?

Re: Different backgrounds for main menu items

Posted: Fri Dec 07, 2007 10:31 am
by faglork
declanreynolds wrote: Hi,

I tried this out and it finds the default image but it doesn't find an image with the same name as an alias.
Double-check the SERVER PATH:

Code: Select all

$filename = '/server/path/to/your/uploads/images/backgrounds/'.$picname.'.jpg';
this is something like
/www/110/home/html/uploads/images/backgrounds/

NOT /uploads/images/backgrounds/
NOT www.yyyy.com//uploads/images/backgrounds/

Look in your config.php - you find the server path to your root directory in
$config['root_path']=/www/zzzz/vvv
so your server path to the backgrounds directory as given in the example would be
/www/zzzz/vvv/uploads/images/backgrounds
declanreynolds wrote:
if the alias is 'aboutus' then do I simply call the image 'aboutus.jpg'?
Yes. All lower case.

hth,
Alex

Re: Different backgrounds for main menu items

Posted: Sun Dec 09, 2007 3:56 am
by declanreynolds
I'm pretty sure the path is correct as it finds the default image. It doesn't find the images that are named the same as the top levels.

This is my path:

$picname =$classes[0];
$filename = '/uploads/images/banners/'.$picname.'.jpg';
if (file_exists($filename)) {
    echo '/uploads/images/banners/'.$picname.'.jpg';
} else {
    echo '/uploads/images/banners/banner_girls.jpg';
}

You can see here: http://mrc.iformat.com.au/ that there is a picture of some girls and this is banner_girls.jpg

If you click 'Settlement Services' this goes to http://mrc.iformat.com.au/index.php?pag ... t-services

Its alias is services-and-information-2

I have an image called services-and-information-2.jpg but it doesn't find it.

I'm sure I'm doing something wrong but I don't know what.

Thanks for your time.

Re: Different backgrounds for main menu items

Posted: Sun Dec 09, 2007 7:53 pm
by faglork
declanreynolds wrote: I'm pretty sure the path is correct as it finds the default image. It doesn't find the images that are named the same as the top levels.

This is my path:

$picname =$classes[0];
$filename = '/uploads/images/banners/'.$picname.'.jpg';
PLEASE re-read my post. This is NOT a server path. It will only work with a "server path". What you specified is a path relative to document root. This is NOT what you need.

Cheers,
Alex

Re: Different backgrounds for main menu items

Posted: Sun Dec 09, 2007 10:43 pm
by calguy1000
BTW, the CustomContent module (version 1.4.10) has these abilities built in..... so if you're using it anyways, you could do stuff like:

Code: Select all

{capture assign='root_alias'}{$ccuser->get_root_alias()}{/capture}
{capture assign='root_title'}{$ccuser->get_page_title($root_alias)}{/capture}
{capture assign='parent_alias'}{$ccuser->get_parent_alias()}{/capture}
{capture assign='parent_title'}{$ccuser->get_page_title($parent_alias)}{/capture}
[edit]

If you put the above code in the head part of your page template, you could simply do a:

Code: Select all

</__body id="{$root_alias}">
  <!-- the rest of my content -->
<__body>
in the body of your page template.... and set the background image, or background color any way you wanted for each root page.