Drupal文档教程翻译 - 覆写输出 - template suggestion(3)

logo-drupal-documentation本站原创编译,转载请给面子,尊重劳动果实,欢迎交流指正。

各位早;天气有些不给力,熬到公司团在座位里面开始做Drupal文档中文翻译先。在前面两天的模板suggestion话题中,我们依次了解了模板suggestion的大致概念原理以及怎样为不同内容类型的页面分配不同的模板文件

这篇文档实际的内容已然到此结束,不过原文的评论部分中,热心用户们贡献了更加实际有用的suggestion代码;我个人在做HTML5之路一类的站点时有用到,顺便也就摘过来作为这个话题的最后一篇,希望可以有参考价值。

根据URL路径(URL path)判断

需求是对于形如“www.example.com/portfolio”的URL调用page-portfolio.tpl.php模板,其他页面仍使用默认的page.tpl.php;后面有简略版供比较参考。

<?php
function phptemplate_preprocess_page(&$variables) {
global $user;
if (module_exists('path')) {
//allow template suggestions based on url paths.
$alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
if ($alias != $_GET['q']) {
$suggestions = array();
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$suggestions[] = $template_filename;
}
$alias_array = explode('/', $alias);
$variables['template_files'] = $suggestions;
}

// Add a single suggestion.
if (module_invoke('throttle', 'status') && isset($user->roles[1])) {
$variables['template_file'] = 'page-busy';
}

// Add multiple suggestions.
if (!empty($user->roles)) {
foreach ($user->roles as $role) {
$filter = '![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s';
$string_clean = preg_replace($filter, '-', drupal_strtolower($role));
$variables['template_files'][] = 'page-'. $string_clean;
}
if (drupal_is_front_page()) {
$variables['template_file'] = 'page-front';
}
}
}
}
?>

根据URL路径(URL path)判断 - 简略版

<?php
function phptemplate_engine_preprocess_page(&$variables) {
$alias = drupal_get_path_alias($_GET['q']);
if ($alias != $_GET['q']) {
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$variables['template_files'][] = $template_filename;
}
}
}
?>

根据某个指定的内容类型判断

<?php
function my_theme_preprocess_page(&$variables) {
if ($variables['node']->type == "my_content_type") {
$variables['template_files'][] = 'page-node-my_content_type';
}
}
?>

根据多个指定的内容类型判断

<?php
function my_theme_preprocess_page(&$variables) {
if ($variables['node']->type == "my_content_type") {
$variables['template_files'][] = 'page-node-my_content_type';
} elseif ($variables['node']->type == "my_content_type_2") {
$variables['template_files'][] = 'page-node-my_content_type_2';
} elseif ($variables['node']->type == "my_content_type_3") {
$variables['template_files'][] = 'page-node-my_content_type_3';
} elseif ($variables['node']->type == "my_content_type_4") {
$variables['template_files'][] = 'page-node-my_content_type_4';
}
}
?>

根据不同的词汇表(vocabulary)判断

这段代码不是出自本篇文档,找不到出处了,用途类似,一并扔过来。

function my_theme_preprocess_page(&$variables) {
if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
$tid = arg(2);
$sql = "Select vid from {term_data} WHERE tid = '%d'";
$vid = db_result(db_query($sql, $tid));
$variables['template_file'] = 'page-vocab-' . $vid;
}
}

命名模板文件时需要使用“page-vocab-”前缀,并链接指定的词汇表id,如“page-vocab-1.tpl.php”。

译者:Viiiix7210

如需转载,请注明官方英文文 档及本人译文的出处,谢谢。查看英文原文: Working with template suggestions


收藏与分享