任何节点的Drupal主题模板文件 [英] Drupal main theme template file for any node

查看:140
本文介绍了任何节点的Drupal主题模板文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何切换到我想要的任何节点的不同主题模板文件?
我了解如何为具有recipes路径的节点创建子主题,如node-recipes.tpl.php。但是我想要控制整个基本模板,如page.tpl.php。
我可以在template.php中使用一些预处理函数吗?

How can I switch to a different theme template file for any node that I want? I understand how to create sub-themes like node-recipes.tpl.php for a node that has a path of "recipes". But what I want to have control of the entire base template like page.tpl.php. Can I use some preprocess function in template.php for this?

现在我在我的template.php文件中有:

Right now I have this in my template.php file:

function mythemename_preprocess_node(&$vars) {

  // template name for current node id
  $suggestions = array('node-'. $vars['nid']);

  // additional node template names based on path alias
  if (module_exists('path')) {
    // we already can have a path alias
    if (isset($vars['path'])) {
      $alias = $vars['path'];
    }else{
      // otherwise do standard check
      $alias = drupal_get_path_alias('node/'. $vars['nid']);
    }

    if ($alias != 'node/'. $vars['nid']) {
      $add_path = '';
      foreach (explode('/', $alias) as $path_part) {
        $add_path .= !empty($path_part) ? $path_part.'_' : '';
        $suggestions[] = 'node-'. $add_path;
      }
      // adding the last one (higher priority) for this path only
      // node-some-long-path-nofollow.tpl.php (not for anchestors)
      $suggestions[] = end($suggestions) .'-nofollow';
    }

    $suggestions=array_map(stripTag, $suggestions);
    //print_r($suggestions);

  }
  $vars['template_files'] = isset($vars['template_files']) ? array_merge($vars['template_files'], $suggestions) : $suggestions;
}

谢谢

推荐答案

是的,

您可以完全控制$ vars ['template_files']数组。我总是建议添加到数组,而不是完全覆盖。

You can fully control the $vars['template_files'] array. I always suggest adding onto the array rather then overwriting it completely.

我有一个我认为的模块,增加了我经常使用的一些小建议。
http://github.com/electblake/template_suggestions/blob/ master / template_suggestions.module

I have a module that I maintain that adds a few small suggestions that I use often. http://github.com/electblake/template_suggestions/blob/master/template_suggestions.module

您可以在preprocess_node,preprocess_page等操作$ vars ['template_files']数组。

You can manipulate the $vars['template_files'] array in preprocess_node, preprocess_page, etc.

如果要将page.tpl.php切换到另一个主题文件,请在preprocess_page hook ...

If you want to switch you page.tpl.php to another theme file do it in the preprocess_page hook...

这篇关于任何节点的Drupal主题模板文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆