为节点类型、术语、章节、路径、首页定制主题(Drupal4.x)

注意:对于Drupal 5,你可参看相应的文章http://drupal.org/node/104316

 

你可以为单篇文章、URL路径、分类术语、章节、和你的首页定制主题。

对于节点、页面和其它的主题定制的更全面的例子,可参看手册的PHP Template Snippets >> Customising full page layouts and sections

 

一种方式是使用sections module,来为你站点的不同部分使用不同的主题。然而,如果你的自定义主题或者模块拥有自己的主题模板文件的话,比如event(事件)模块,它需要许多重复的CSS和图片资源。

 

在一个Drupal站点上使用多个主题的关键是,由主题引擎决定当前你在站点中所处的位置。下面是我们可以从模板引擎中检查的一列条件。

 

$is_front phptemplate变量可用来检查当前是否处于首页。

<?php
if ($is_front) {
  include('front.tpl.php');
  return;
}
?>

 

Drupal的arg()函数用来得到路径中的参数。

arg(0)=="admin"// is /admin
arg(0) =="node"// is /node
arg(0)=="user" // is /user
arg(0)=="node"&&arg(1)=="add" // is /node/add
arg(0)=="node"&& arg(2)=="edit" // is /node/###/edit
arg(0)=="user"&&arg(1)=="add" // is /user/add
arg(0)=="admin"&&arg(1)="user"&&arg(2)=="create" // is /admin/user/create
arg(0)=="alias"&&arg(1)=="alias1" is /alias/alias1
arg(0)=="taxonomy"&&arg(1)=="term"&&arg(2)=="term#" // is /taxonomy/term/term#

//arg(1)=="comment"
//arg(2)=="reply"

 

推荐阅读根据节点类型定制你的节点模板

 

一旦你知道了front_page的值,或者路径,那么我们就可以使用不同的主题模板,也可使用CSS文件,或者修改xHTML标签的CSS类了。本节将分成3个子页面。

 

  1. 1,使用一个特定的模板,比如front.tpl.php, admin.tpl.php, term#.tpl.php。

<?php
if ($is_front) {
    include 'front.tpl.php';
    return;
}
elseif (arg(0)=="taxonomy"&&arg(1)=="term"&&arg(2)=="term#") { // add argument of choice
    include 'term#.tpl.php';// add template of choice
    return;
}

 

  1. 2,另一种选择是,为站点的不同部分使用不同的CSS。在下面的例子中,我们将基于路径来决定当前所在的位置,然后使用不同的CSS。在下面的例子中,我们决定要添加哪一个CSS。

<?php
if ($is_front) {
    include 'page_front.tpl.php';
    return;
}
if ($node->type == 'nodetype') { // all pages on my site are 'nodetype'
    $my_path = explode("/", drupal_get_path_alias('node/'.$node->nid)); // all pages have path like 'section/page'
    $my_path = $my_path[0];
} else {
    $my_path = arg(0);
}
switch ($my_path) {
    case 'using':
        $section = 1;
        break;
    case 'education':
        $section = 2;
        break;
    case 'company':
        $section = 3;
        break;
    case 'image':
        $section = 4;
        break;
    case 'forum':
        $section = 5;
        break;
    default:
        $section = 0;
}
$css_section = 'section'.$section;
?>

  1. 3,我们也可以为要被主题化的xHTML直接添加CSS类。

PHPTemplate 主题:

在page.tpl.php的body标签中(来自于bluemarine):
<body <?php print $onload_attributes ?> >

将变为:
<body class=" <?php print arg(0); ?> " <?php print $onload_attributes ?> >

接着你就可以为管理页面使用.admin作为css父选择器了。

 

对于Smarty

在page.tpl的body标签中(来自于bluemarine_smarty):
<body{$onload_attributes}>

将变为:
<body class="{php} echo arg(0); {/php}"{$onload_attributes}>

 

当查看根节点时,这可能产生一个空的class属性值---可以在class中包含一个固定的前缀,从而阻止这种情况的发生。

drupal节点匹配主菜单链接

下面的代码片断,用来检查当前节点是否匹配一个主菜单链接.这段代码假定主菜单链接的格式为'/somepage'.

 

<?php if (count($primary_links)) : ?>
    <ul class="primary">
        <?php foreach ($primary_links as $link): ?>
            <?php preg_match("/<a\s*.*?href\s*=\s*['\"]([^\"'>]*).*?>(.*?)<\/a>/i", $link, $matches); ?>
            <?php if (('/'.drupal_get_path_alias('node/'.$node->nid))==$matches[1]): /* if $node exists */ ?>
                <li class="selected"><?php print $link?></li>
            <?php elseif ('/'.arg(0)==$matches[1]): /* else try to use arg */ ?>
                <li class="selected"><?php print $link?></li>
            <?php elseif ((drupal_get_path_alias('node/'.$node->nid)=='node/') && (arg(0)=='node') && ($matches[1]=='/')): /* else if home */ ?>
                <li class="selected"><?php print $link?></li>
            <?php else: ?>
                <li><?php print $link?></li>
            <?php endif; ?>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

 

在这段代码中,我们只使用了arg(0)进行检查,对于那些由drupal模块创建的类节点来说,可能并不适用.

 相关链接: http://drupal.org/node/45954 , Think in Drupal

Drupal版本:

如何为每个drupal页面生成<body> class/id属性

这个代码将为你drupal站点上的每个页面的<body>标签生成一个类(class)和id。

在你主题的页面模板文件(page.tpl.php)中,将已存在的<body>标签替换为下面的代码:

<body<?php print phptemplate_body_attributes($is_front, $layout); ?>>

      

并在你主题的template.php文件中添加下面的代码:

 

/**

* Sets the body tag class and id attributes.

*

* From the Theme Developer's Guide, http://drupal.org/node/32077

*

* @param $is_front

*   boolean Whether or not the current page is the front page.

* @param $layout

*   string Which sidebars are being displayed.

* @return

*   string The rendered id and class attributes.

*/

function phptemplate_body_attributes($is_front = false, $layout = 'none') {

 

  if ($is_front) {

    $body_id = $body_class = 'homepage';

  }

  else {

    // Remove base path and any query string.

    global $base_path;

    list(,$path) = explode($base_path, $_SERVER['REQUEST_URI'], 2);

    list($path,) = explode('?', $path, 2);

    $path = rtrim($path, '/');

    // Construct the id name from the path, replacing slashes with dashes.

    $body_id = str_replace('/', '-', $path);

    // Construct the class name from the first part of the path only.

    list($body_class,) = explode('/', $path, 2);

  }

  $body_id = 'page-'. $body_id;

  $body_class = 'section-'. $body_class;

 

  // Use the same sidebar classes as Garland.

  $sidebar_class = ($layout == 'both') ? 'sidebars' : "sidebar-$layout";

 

  return " id=\"$body_id\" class=\"$body_class $sidebar_class\"";

}

 

注意:这里你需要启用简洁url,才能正常使用。

 

使用例子:

使用了这段代码后,当你访问news/archive/2007页面时,你将看到下面的<body>属性:

<body id="page-news-archive-2007" class="section-news sidebar-left">

现在你要做的就是为整个站点的新闻栏目创建一套新的CSS规则:

.content { background-image: url("background.png"); }

.section-news .content { background-image: url("news.png"); }

 

如果你启用了路径别名模块的话,并且花了功夫为你的页面创建了多层级的路径,这样你就很容易将站点分为做个栏目,然后为每个栏目使用一个完全不同的布局(背景图片,颜色,宽度等等)。

 相关联接: http://drupal.org/node/32077 ,  Think in Drupal

Drupal版本: