Drupal 7 - template.php 中指示使用哪个页面模板的变量是什么?

发布于 2024-10-31 13:00:58 字数 1359 浏览 3 评论 0原文

好的,事情是这样的:我正在构建一个包含几个不同部分的 Drupal 网站。每个部分都是一个显示内容类型的视图。 (每个部分都有自己的内容类型)例如,我有一个指向 ?q=blog 的视图,它显示内容类型 blog

所有部分看起来都略有不同。与“网站内的网站”不同,但差异足够大,以至于它们不能全部使用相同的模板文件并使用 CSS 进行修改。每个部分都需要它自己的page.tpl.php

不幸的是,据我所知 Drupal 主题的 .info 文件只能为整个主题分配一个 page.tpl.php 或分配一个 page-node-####.tpl.php > 对于每个节点。该网站上会有很多内容,因此设置 Drupal 为每个创建的节点创建一个新的相同的 page-node-####.tpl.php 很快就会变得难以管理。

为了解决这个问题,我将使用 pathauto 为每种内容类型创建一个别名。例如,内容类型 blog 的所有节点都被赋予别名 ?q=blog/[post title]。修改 template.php 以将 page-blog.tpl.php 用于别名以“blog”一词开头的任何页面。

其他人已经尝试过做这类事情并创建了诸如所描述的功能。不幸的是,我看到的所有都是针对 Drupal 6 或更低版本的。我尝试修改现有的但没有成功。但到目前为止,我认为这是正确的:

function basic_preprocess_page(&$vars, $hook) {
    ...

    if( module_exists('path') ) {
        $alias = drupal_get_path_alias( $_GET['q'] );
        $site_section = "blog";
        if( strpos( $alias, $site_section ) === 0 ) {
            $VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE = "/path/to/page-blog.php";
        }
    }
}

我找不到 $VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE 有人知道它是什么吗?

也许我的网站结构很糟糕。如果有人知道如何重组我的网站,以便我可以更轻松地制作具有单独部分的主题,请分享如何!

谢谢一百万! (c:

编辑:也许我需要使用模板建议。有谁知道用于设置它的函数或变量吗?

Ok, here's the deal: I am constructing a Drupal website that has several different sections. Each section is a view that displays a content type. (Each section has it's own content type) For example, I have a view that points to ?q=blog which displays content type blog.

All the sections look a little different than each other. Not like 'website-within-a-website' different but different enough that they can't all use the same template file and each be modified with CSS. Each section needs it's own page.tpl.php.

Unfortunately, AFAIK Drupal theme's .info files can only either assign one page.tpl.php for the entire theme or assign a page-node-####.tpl.php for each node. There is going to be lots of content on this website so setting Drupal to make a new identical page-node-####.tpl.php for every created node would get unmanagable very fast.

To solve this problem, I am going to use pathauto to create an alias for each content type. For example, all nodes of content type blog are given an alias ?q=blog/[post title]. Modify template.php to use page-blog.tpl.php for any page who's alias starts with the word 'blog'.

Other people have tried doing this sort of thing and have created functions such as the one described. Unfortunately, all the ones I have seen are for Drupal 6 or below. I have tried modifying existing ones with no success. So far, though, I think this is on the right track:

function basic_preprocess_page(&$vars, $hook) {
    ...

    if( module_exists('path') ) {
        $alias = drupal_get_path_alias( $_GET['q'] );
        $site_section = "blog";
        if( strpos( $alias, $site_section ) === 0 ) {
            $VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE = "/path/to/page-blog.php";
        }
    }
}

I cannot find $VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE does anyone know what it is?

Maybe my site is structured badly. If anyone knows how to restructure my site so I can more easily make a theme with seperate sections please share how!

Thanks a million! (c:

EDIT: Perhaps I need to use template suggestions instead. Does anyone know the function or variable to use to set this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不离久伴 2024-11-07 13:00:58

他们在 D7 中更改了这个数组键的名称,但我没有在任何地方看到它的记录。经过一番调试后,我终于弄清楚了这一点。您可以使用 hook_preprocess_page() 覆盖 template.php 中的主题模板,如下所示:

function myTheme_preprocess_page(&$vars) {
global $node;

if ($node->type == 'blog') {
     $vars['theme_hook_suggestions'] = array('my__blog_template'); // use my--blog-template.tpl.php, note '-' = '_'
}
elseif ($node->type == 'articles') {
    $vars['theme_hook_suggestions'] = array('article__node_template'); // use article--node-template.tpl.php
}
}

哦,对 template.php 进行更改后,不要忘记刷新 Drupal 缓存。

They changed the name of this array key in D7 and I haven't seen it documented anywhere. I finally figured this out after a good bit of debugging. You can override the theme template in template.php with a hook_preprocess_page() like so:

function myTheme_preprocess_page(&$vars) {
global $node;

if ($node->type == 'blog') {
     $vars['theme_hook_suggestions'] = array('my__blog_template'); // use my--blog-template.tpl.php, note '-' = '_'
}
elseif ($node->type == 'articles') {
    $vars['theme_hook_suggestions'] = array('article__node_template'); // use article--node-template.tpl.php
}
}

Oh and don't forget to flush the Drupal caches after making changes to your template.php.

×眷恋的温暖 2024-11-07 13:00:58

好的,我找到了:
http://drupal.org/node/223440#comment-991840

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

归功于此功能转到用户 mfb。

我在这方面遇到了很多麻烦,所以我会在这里解释一下,以防有人觉得它有用。

该函数位于您的 template.php 中。它需要是 _preprocess_page 函数的一部分。它的作用是获取别名,然后将其分解为一堆不同的组件。例如,如果您所在的页面具有别名 ?q=blog/blog-post-title ,它将分解为 blogblog-post-title。然后,它将每个组件转换为模板建议的名称。它将每个模板建议放入 template_files[] 数组(在 $variables[] 数组内),以便页面现在有两个新的模板建议:

page- blogpage-blog-blog-post-title

模板建议是备用模板文件。在本例中,它们用于页面,但不一定必须如此。您可以为您能想到的任何内容提供模板建议,包括块、节点等。不要让“模板建议”这个名字欺骗了您。只要默认模板存在,模板建议就会优先于默认模板使用。我不知道为什么这么命名。我认为应该重新命名。

既然您已经设置 Drupal 来查找指向您的别名的模板建议,那么您要做的就是创建一个新的模板文件,其中所有其余内容都在您的主题中。在这种情况下,假设我想为我的整个博客部分设置主题。在模板文件夹中,我应该创建一个名为 page--blog.tpl.php (注意 --双连字符--)的文件,其中包含我想要的布局。

Drupal 将使用它能找到的最具体的模板建议,因此如果您愿意,您可以使一篇博客文章看起来与网站的其他部分完全不同,只要您为其创建一个名为 page--blog--blog 的模板-post-title 并将其放入主题的模板目录中。 (再次注意双连字符。)

Ok, I found it:
http://drupal.org/node/223440#comment-991840

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

Credit to this function goes to user mfb.

I had a lot of trouble with this so I will explain it here in case anyone finds it useful.

This function goes in your template.php. It needs to be part of the <theme name>_preprocess_page function. What it does is it takes the alias and then explodes it into a bunch of different components. For example if you are on a page with the alias ?q=blog/blog-post-title it would be exploded into blog and blog-post-title. It then turns each component into a name for a template suggestion. It puts each template suggestion into the template_files[] array (inside the $variables[] array) so that the page now has two new template suggestions:

page-blog, and page-blog-blog-post-title

Template suggestions are alternate template files. In this case they are for pages, but they don't necessarily have to be. You can have template suggestions for anything you can think of including blocks, nodes and the like. Don't let the name 'template suggestion' fool you. Template suggestions will be used over default templates as long as they exist. I don't know why it was named like that. I think it should be renamed.

What you do, then, now that you've set up Drupal to look for a template suggestion that points to your alias, is create a new template file where all the rest are in your theme. In this case, let's say I want to theme my entire blog section. In the templates folder I should create a file named page--blog.tpl.php (note the --double hyphens--) with the layout I want.

Drupal will use the most specific template suggestion it can find so if you wanted you could make one blog post to look completely different than the rest of the site long as you make a template for it named page--blog--blog-post-title and put it in your theme's templates directory. (again, note the double hyphens.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文