在 Drupal 7 中为另一个页面使用 front.tpl [英] Using front.tpl in Drupal 7 for another Page

查看:11
本文介绍了在 Drupal 7 中为另一个页面使用 front.tpl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很直接的问题:

我在 drupal 7 站点中使用了 page--front.tpl 和 page.tpl 模板页面.但是,我想在另一页上使用 page-front.tpl.这是可能的还是我需要创建另一个 .tpl 页面来执行此操作.

I have page--front.tpl and page.tpl template pages in use in a drupal 7 site. However, I'd like to use page-front.tpl on one other page. Is this possible or do I need to create another .tpl page to do this.

我正在开发的网站分为两个部分,它本质上是两个独立的网站,您可以在您是消费者还是企业主之间切换.所以我想对每个站点的主页使用front.tpl模板.

The site I'm working on is split into two sections, it's essentially two seperate websites that you can flip between whether your a consumer or business owner. So I want to use the front.tpl template for the home page of each site.

干杯.

推荐答案

您可以将 theme_preprocess_page 函数添加到您主题的 template.php 文件中,然后将您的模板名称添加到模板建议列表.

You can add theme_preprocess_page function to your theme's template.php file and then add your template name in templates suggestions list.

function mytheme_preprocess_page(&$vars) {
    // you can perform different if statements
    // if () {...
        $template = 'page__front'; // you should replace dash sign with underscore
        $vars['theme_hook_suggestions'][] = $template;
    // }
}

编辑

如果你想通过路径别名指定模板名称,你可以写这样的代码:

If you want to specify template name by path alias, you could write code like this:

function phptemplate_preprocess_page(&$variables) {
    if (module_exists('path')) {
        $alias = drupal_get_path_alias($_GET['q']);
        if ($alias != $_GET['q']) {
            $template = 'page_';
            foreach (explode('/', $alias) as $part) {
                $template.= "_{$part}";
                $variables['theme_hook_suggestions'][] = $template;
            }
        }
    }
}

如果没有此功能,您将默认获得以下节点模板建议:

Without this function you would have the following node template suggestions by default:

array(
    [0] => page__node
    [1] => page__node__%
    [2] => page__node__1
)

此功能将适用于您的节点以下新模板建议.带有 node/1 路径和 page/about 别名的示例节点:

And this function would apply to your node the following new template suggestions. Example node with node/1 path and page/about alias:

array(
    [0] => page__node
    [1] => page__node__%
    [2] => page__node__1
    [3] => page__page
    [4] => page__page_about
)

然后你就可以在你的页面上使用page--page-about.tpl.php.

So after that you can use page--page-about.tpl.php for your page.

如果你想将page--front.tpl.php应用到你的node/15,那么在这个函数中你可以添加if语句.>

If you want to apply page--front.tpl.php to your let's say node/15, then in this function you can add if statement.

function phptemplate_preprocess_page(&$variables) {
    if (module_exists('path')) {
        $alias = drupal_get_path_alias($_GET['q']);
        if ($alias != $_GET['q']) {
            $template = 'page_';
            foreach (explode('/', $alias) as $part) {
                $template.= "_{$part}";
                $variables['theme_hook_suggestions'][] = $template;
            }
        }
    }

    if ($_GET['q'] == 'node/15') {
        $variables['theme_hook_suggestions'][] = 'page__front';
    }
}

这将为您提供以下模板建议:

This would give you the following template suggestions:

array(
    [0] => page__node
    [1] => page__node__%
    [2] => page__node__1
    [3] => page__page
    [4] => page__page_about
    [5] => page__front
)

最高索引 - 最高模板优先级.

The highest index - the highest template priority.

这篇关于在 Drupal 7 中为另一个页面使用 front.tpl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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