【问题标题】:Drupal main theme template file for any node任何节点的 Drupal 主题模板文件
【发布时间】:2010-07-06 19:25:40
【问题描述】:

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

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

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;
}

谢谢

【问题讨论】:

    标签: drupal templates themes overriding preprocessor


    【解决方案1】:

    是的,

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

    我有一个我维护的模块,其中添加了一些我经常使用的小建议。 http://github.com/electblake/template_suggestions/blob/master/template_suggestions.module

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

    如果您想将 page.tpl.php 切换到另一个主题文件,请在 preprocess_page 挂钩中执行...

    【讨论】:

    • 谢谢,我会检查的。如果我想为具有“news”的url路径的“page”类型的节点使用特殊的模板文件,我将如何实现它?我会在 "($hook == 'page')" if 语句中添加一些代码来检查具有这样别名的模板: node-news.tpl.php 或 news.tpl.php 吗?谢谢
    • 该函数“template_suggestions_preprocess”可以在template.php文件中使用还是必须在模块中使用?
    • 1) 我通常忽略节点的 url,我只使用 hook_preprocess 和 $vars['node']->type。这使我能够根据需要更改任何 pathautos 等。在 hook_preprocess_page() 的情况下,您可以在 $vars 变量中看到页面的 url,我必须查看但快速 dpm($vars) 应该在底部附近显示它。
    • 2) 如果它在 template.php 中,它将是 _preprocess();
    【解决方案2】:

    我正在使用此功能来创建适合我的模板建议。谢谢大家的建议。

    /**
     * Override or insert PHPTemplate variables into the templates.
     * These are the main outer templates such as page.tpl.php 
     */
    function phptemplate_preprocess_page(&$vars) {
    
        $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;
                $vars['template_files'][] = $template_filename;
            }
        }
        //----  
    
    //print_r(arg()); 
    
    /*  print '<pre>';
      print_r($vars);
      print '</pre>';*/
    
      //dpm($vars);
      //print_r($vars['template_files']);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多