I'm coming rather close to finishing work on my first Drupal based website and I have to say that a combo of drupal and phptemplate is very nice.

The website I'm working on will have 5 basic sections. They are distinctly different from one another and to help keep visitors oriented I decided to make the design's color scheme change depending on which section they are in. This is achieved quite easily by linking to a final stylesheet in "head" that overrides (or adds to) previous color/image declarations in style.css. This is essentially taking a page from the CSS Zen Garden by keeping structure and appearance almost entirely separate.

My template is currently hard coded to reference this 3rd stylesheet.

Every node is attached to a single term, which exists in a vocabulary of the same name.

So what I need to know is how I should go about replacing the link to style.home.css with a given section's stylesheet (say "style.about.css") when a visitor goes to a node that exists in that section (vocabulary) or when they go directly to that vocabulary's term.

I should note that none of the nodes on the site will have more than one term attached to them and thus only exist in one vocabulary, still this may be difficult to do simply because nodes can be attached to more than one term and those terms can sometimes exist in more than one vocabulary.

If at all possible I'd like to stay away from other solutions that require more than one template and then call a different template based on which vocabulary or node they are in, this isn't very elegant because it's more complicated for the site administrator and more difficult to keep each theme up to date.

Thanks for the help, it's much appriciated!

Comments

patrickharris’s picture

I'd like to know how to do this as well.

An interim mesaure could be to give each page a <body id="blah"> with the name of each section (worked out by checking $preAlias = $_SERVER['REQUEST_URI']; for the relevant section).

DittoBox’s picture

I'd like to do something akin to this, it seems to be the easiest (in my small mind anywho) way of accomplishing my goal.

Have a bit of PHP inside the beginning of the document, where I've got the stuff that checks to see if we're in admin (and then includes admin.tpl.php) that gets $_SERVER['REQUEST_URI'] then chops that up and takes only section from /sectionfoo/nodebaz (each node has sectionname/nodename in a rewrite alias via the path module) from it, turns that resulting "section" information into $css_section. We then do this where we want it to spit out the theme dir and css section name:

<link href="<?php print $directory ?>/<?php print $css_section?>.css" rel="stylesheet" type="text/css" media="all" />

Obviously each CSS file is then named for each approriate section.

My problem is that I don't have the time to learn this kind of stuff about PHP (I plan to look into it after I get this up and running) right now, so if anyone could help me get it working I'd be extremely grateful.

Thanks!

patrickharris’s picture

Hey DittoBox, the links you gave in another thread described use of arg(0). It seems in the page template it would be simpler to merely use arg(0) or arg(1) etc, than bother with $_SERVER['REQUEST_URI'] & then chop it into sections (which I had been doing previously).

If I wanted to use a completely new css sheet for a section, I would link to it using arg(0). However, if I just wanted to override a few css specifications (like the width of one container or something), I would be tempted to assign the body tag an ID of arg(0) to do my few overrides in the original stylesheet.

patrickharris’s picture

arg(0) doesn't show aliased urls, so I guess it's back to $_SERVER['REQUEST_URI'] unless someone knows a better way?

stevensj2’s picture

What I do is not necessarily better, but is an option.

I make it so:<body ="home'$title'">
so that the body is the title of whatever node is being viewed, while also having a unique id for the very front page (would be just "home").

Then I create CSS for the node titles I want (usualy pages only), and I mostly use this type of thing to achieve tabs that indicate where the user is in the site.
-----------------
Josh Stevens
Nautilus7 Design | Buy Drupal T-Shirts, Mousepads, and more!

DittoBox’s picture

$section = $_SERVER['REQUEST_URI'];
$css_section = explode("/", $section);

In the header and this in the CSS referer:

<link href="<?php print $directory ?>/<?php print $css_section[1] ?>.css" rel="stylesheet" type="text/css" media="all" />

EDIT:
This works, but I need to make it only print $section[1] if the section is listed in the if statement below, but it's not working, it prints the section[1] regardless of what section we're in...what's wrong?:

<?php $uri_request_id = $_SERVER['REQUEST_URI'];
$section = explode("/", $uri_request_id);

if ($section[1] == (home) || (about) || (gallery))
	$css_section = $section[1];
else
	$css_section = "default"; ?>
patrickharris’s picture

I'm not a PHP expert, but it must be something to do with your if statement.

This works:

if (($section[1] == "recording") or ($section[1] == "about") or ($section[1] =="gallery")) {
	 $css_section = $section[1];
	}  else
{$css_section = "default";}
DittoBox’s picture

It work's great! Thanks a ton!!!

$uri_request_id = $_SERVER['REQUEST_URI'];
$section = explode("/", $uri_request_id);

if (($section[1] == "home") or ($section[1] == "ministries") or ($section[1] =="events") or ($section[1] =="about") or ($section[1] =="contact")) {
	$css_section = $section[1];
} else
	{$css_section = "home";}

Again, thanks!

I'm off to go buy a book on PHP now.

Prometheus6’s picture

If I were to do this, I think I would use a PHPTemplate-based theme. In the theme I'd create a template.php file where I would parse out the page to be displayed and create a new variable holding the custom css file name. Then I'd add that to the page.tpl.php file.

patrickharris’s picture

Hi Prometheus6,
I vaguely understand that by using template.php, I can override functions (by replacing them with new ones), and also pass variables to my templates, I think by using function _phptemplate_variables($hook, $vars).

Can you explain more fully what you mean though?

Prometheus6’s picture

I'll try...a free-form example looks like suspicious imput to this system.

Here's the example from the handbook page:

function  _phptemplate_variables($hook, $vars) {
   switch($hook) {
     case 'comment' :
        $vars['newvar'] = 'new variable';
        $vars['title'] = 'new title';
        break;
   }
   return $vars;
}

In this situation, we're looking to work with the 'page' hook. You'd grab the requested URL however you need to...even if you're using aliases I'd use the arg() function.

So say you find you're working with taxonomy id number 6. You can assign (say) taxonomy_6.css to (say) $vars['taxonomy_css'].

Then, in your page.tpl.php file, after the
print $styles;
statement in th ehead section, you add a statement that builds a stylesheet link out of the value in $taxonomy_css

patrickharris’s picture

How would I find I'm working with taxonomy id number 6 from the arg() function? The arg() returns the un-aliased url, which will just be something like node or 32.

micha_1977’s picture

pls delete this comment

Prometheus6’s picture

The unaliased URL for a taxonomy-filtered listing is something like

http://www.example.com/taxonomy/term/5

So you pluck your taxonomy id off the end. It gets hairier if your sections are aliased unions of categories:

http://www.example.com/taxonomy/term/5,6

You'd have to use that $_SERVER['REQUEST_URI'] variable. But the point is, the template.php file puts the figuring out in a single place that you can take with you is you change themes or implement it elsewhere...and the changes to the theme page template become really simple.