页面模板建议不起作用


12

我已经创建了一个主题,并在此结构中包含了模板文件

  • /templates/page/page.tpl.php
  • /templates/page/page--node-type.tpl.php

我已经创建了一个自定义页面模板,但是由于某种原因,Drupal并未使用它。我已经清除了缓存,还尝试在主题template.php文件中添加此预处理器功能,但仍无法正常工作。

if (isset($vars['node'])) 
  {
    // If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
    $vars['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $vars['node']->type);
  }

任何帮助,将不胜感激。


/templates/page/page--node-type.tpl.php应该不是page--blog.tpl.php吗?
杰里米·法兰西

Answers:


14

Drupal 7模板建议中所述,Drupal 7默认使用的页面模板建议是page-[front | internal / path] .tpl.php。

对于http://www.example.com/node/1/edit上可见的页面,Drupal将寻找以下模板文件:

  • 页面-节点--edit.tpl.php
  • 页面--node--1.tpl.php
  • 页面--node.tpl.php
  • page.tpl.php

要添加其他建议,您的主题应实现template_preprocess_page()并在中添加新建议$variables['theme_hook_suggestions']$variables是通过引用传递给函数的变量)。

如果这样做,那么未使用建议的模板文件的唯一原因是因为该文件的名称不正确:例如,在该页面显示书页的情况下,模板文件应为page--book.tpl .php。您可以更改主题的代码,如果找不到页面–book.tpl.php之类的模板,则可以使用页面-node-type.tpl.php模板。

还要注意的是,在theme_get_suggestions()(由template_preprocess_page()调用的函数)中,连字符被代替_,反之亦然。在功能代码中报告的注释中解释了这样做的原因。

// When we discover templates in drupal_find_theme_templates(),
// hyphens (-) are converted to underscores (_) before the theme hook
// is registered. We do this because the hyphens used for delimiters
// in hook suggestions cannot be used in the function names of the
// associated preprocess functions. Any page templates designed to be used
// on paths that contain a hyphen are also registered with these hyphens
// converted to underscores so here we must convert any hyphens in path
// arguments to underscores here before fetching theme hook suggestions
// to ensure the templates are appropriately recognized.
$arg = str_replace(array("/", "\\", "\0", '-'), array('', '', '', '_'), $arg);

5

我正在使用Drupal 7.4,但我遇到了同样的问题,而唯一有帮助的是这篇文章:如何基于内容类型添加自定义page.tpl

从帖子:

<?php
/**
* Variables preprocess function for the "page" theming hook.
*/
function THEME_NAME_preprocess_page(&$vars) {

  // Do we have a node?
  if (isset($vars['node'])) {

    // Ref suggestions cuz it's stupid long.
    $suggests = &$vars['theme_hook_suggestions'];

    // Get path arguments.
    $args = arg();
    // Remove first argument of "node".
    unset($args[0]);

    // Set type.
    $type = "page__type_{$vars['node']->type}";

    // Bring it all together.
    $suggests = array_merge(
      $suggests,
      array($type),
      theme_get_suggestions($args, $type)
    );

    // if the url is: 'http://domain.com/node/123/edit'
    // and node type is 'blog'..
    //
    // This will be the suggestions:
    //
    // - page__node
    // - page__node__%
    // - page__node__123
    // - page__node__edit
    // - page__type_blog
    // - page__type_blog__%
    // - page__type_blog__123
    // - page__type_blog__edit
    //
    // Which connects to these templates:
    //
    // - page--node.tpl.php
    // - page--node--%.tpl.php
    // - page--node--123.tpl.php
    // - page--node--edit.tpl.php
    // - page--type-blog.tpl.php          << this is what you want.
    // - page--type-blog--%.tpl.php
    // - page--type-blog--123.tpl.php
    // - page--type-blog--edit.tpl.php
    //
    // Latter items take precedence.
  }
}
?>

非常感谢...显示建议和模板名称之间的关系确实很有帮助。再次感谢:)
SGhosh

2

我花了很长时间尝试按照上面的示例在Drupal 7.22中使用字符串替换。这似乎对我不起作用。有趣的是,某些内容类型似乎是自动建议的,而另一些则不会。这是最终为我工作的代码。

if (isset($variables['node'])) {
   // $variables['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $variables['node']->type);
   //cannot get above working for some reason?
     $variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type;
  }

因此,针对front_page内容类型的模板建议应为:

页面--front_cover.tpl.php

有趣的是,针对“问题”内容类型的代码模板建议作为页面--issue.tpl.php出现,而无需任何预处理程序脚本!就我而言,这似乎覆盖了使用类似路径的视图模板。

查看路径= / issue /#基于内容类型的模板建议,即/ issue /#/ front_cover


对于front_page内容类型的模板建议,它将没有任何预处理程序脚本:page--front-cover.tpl.php
sneha.kamble
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.