Mar 12, 2013 - 前端    No Comments

drupal7自定义html层和page层tpl的命名规则

drupal7在tpl的使用上要比drupal6升级很多,最明显的区别就是有了html.tpl.php这个文件,可以很方便的对一些全局性的信息进行管理。
一个标准的html.tpl.php的代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
  "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php print $language->language; ?>" version="XHTML+RDFa 1.0" dir="<?php print $language->dir; ?>"<?php print $rdf_namespaces; ?>>

<head profile="<?php print $grddl_profile; ?>">
  <?php print $head; ?>
  <title><?php print $head_title; ?></title>
  <?php print $styles; ?>
  <?php print $scripts; ?>
</head>
<body class="<?php print $classes; ?>" <?php print $attributes;?>>
  <div id="skip-link">
    <a href="#main-content" class="element-invisible element-focusable"><?php print t('Skip to main content'); ?></a>
  </div>
  <?php print $page_top; ?>
  <?php print $page; ?>
  <?php print $page_bottom; ?>
</body>
</html>


前段时间在使用drupal7的时候,居然发现bluemasters这个theme没有html.tpl.php这个文件,很简单,去 /modules/system 文件夹下找到html.tpl.php,复制到theme的目录下就可以了。

顺便记录一下两种常用的tpl使用方法:

很多时间,我都希望每个content type能有自己的page层tpl, 其实也很简单,在template.php的预处理函数中做一下判断就可以了,以bluemasters这个theme为例:

function bluemasters_preprocess_page(&$variables) {
  if (!empty($variables['node'])) {
    $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
  }
}

那么只要以page.tpl.php为模板创建出 page–node–article.tpl.php 和 page–node–page.tpl.php 两个文件,默认就存在的两个content type:article和page就都有了自己的page层tpl。

代码还可以象这样扩充:

function bluemasters_preprocess_page(&$variables) {
  if (!empty($variables['node'])) {
    $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
	$variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->nid;
  }
}

这样我们就可以为每一个node单独创建tpl了(page–node–1.tpl.php),这TMD是要多蛋疼的客户才能让我们用到这行代码啊!!

—————-竖中指的分割线—————-

function bluemasters_preprocess_html(&$variables) {
	$node = menu_get_object();
	if ($node && $node->nid) {
		$variables['theme_hook_suggestions'][] = 'html__node__' . $node->type;
		$variables['theme_hook_suggestions'][] = 'html__node__' . $node->nid;
	}
}

每个逼我用到这段代码的客户上辈子都是基佬的养子!

==============最后的加强版==============
用的url alias的情况也很多,这时候就要用到最后的加强代码了:

function bluemasters_preprocess_page(&$variables) {
	if (!empty($variables['node'])) {
		$variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
		$variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->nid;
	}
	//Create page suggestion for first part of url-alias
	$url_alias = drupal_get_path_alias($_GET['q']);
	$parts = explode('/', $url_alias);
	$variables['theme_hook_suggestions'][] = 'page__'.$parts[0].'__alias';

}
function bluemasters_preprocess_html(&$variables) {
	$node = menu_get_object();
	if ($node && $node->nid) {
		$variables['theme_hook_suggestions'][] = 'html__node__' . $node->type;
		$variables['theme_hook_suggestions'][] = 'html__node__' . $node->nid;
	}
	//Create page suggestion for first part of url-alias
	$url_alias = drupal_get_path_alias($_GET['q']);
	$parts = explode('/', $url_alias);
	$variables['theme_hook_suggestions'][] = 'html__'.$parts[0].'__alias';
}

以上!!!

Got anything to say? Go ahead and leave a comment!