I want to be able to theme any node except in a different way depending on the content. Imagine - a red background for an excerpt if it's marked as critical using taxonomy.

I've got the node.tpl doing the right thing. It applies differnet CSS depending on the content.

Now need to share some information between a snippet of php code in a page which creates a list of particular nodes, and the node.tpl.php which themes them. At the moment it looks like the code to run embedded PHP is isolated from the rest of the code, so that you can't overwrite variables. This stops you passing info into and out of the embedded script.

I could alter the way my Drupal works, perhaps making $vars available in the embedded code. But I'm wondering if there's already a way of doing this that I'm missing. Any hints, experts?

Thanks,

Jeff

Comments

micha_1977’s picture

you want different styles for your
nodes ?

its possible by using template.php all alone

ill give you an example php code

function _phptemplate_variables($hook, $vars) {
/***********************************
 * HOOK NODE
 * - frontpage
 *   - override node layout/content + set additional variables depending on page
 */
  if ($hook == 'node') {

    if ($vars['is_front']) {


      /**
        * override node layout + content depending on page
        */
            // check for the template file
      $complete_path = $vars['directory'].'/frontnode.tpl.php';
            // see php.net forproblems with file_exists and user rights
      if (file_exists($complete_path)) {
        $vars['template_file'] = 'frontnode';
      }

    }
}
/* if needed send additional stylesheet along with the new nodetemplate */
  if ($hook == 'page') {
    if ($vars['is_front']) {

      /**
        * add special stylesheets for the same page, as in hook - node
        */
      $stylesheet = $vars['directory'].'/wpclassic.css';
      $vars['styles'] .= theme_stylesheet_import($stylesheet, $media = 'all');
}
}
 // calling function expects an array
  if (empty($vars)) { 
    $vars = array();
  }
  return $vars;
}

this example gives you the opportunity to change
layout for a node (or page, just reproduce it for the page hook) and
.css
delete the one you dont need

in your case, you won't check for "is_front" but for other cases, id dont know if you can get your php logic inside the template file over to the template.php, but if the style is depending on things like

...odd and even count of nodes ?
...the $node->content
...etc.

you could make it my way

important note -> i typed it fast maybe i missed some brackets

-micha
work in progress with Drupal 4.6: langmi.de