Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 将Drupal 7中的front.tpl用于另一个页面_Templates_Drupal_Frontpage - Fatal编程技术网

Templates 将Drupal 7中的front.tpl用于另一个页面

Templates 将Drupal 7中的front.tpl用于另一个页面,templates,drupal,frontpage,Templates,Drupal,Frontpage,非常直截了当的问题: 我在Drupal7站点中使用了page--front.tpl和page.tpl模板页面。但是,我想在另一个页面上使用page-front.tpl。这是可能的,还是我需要创建另一个.tpl页面来执行此操作 我的网站分为两个部分,基本上是两个独立的网站,你可以在你的客户或企业主之间切换。所以我想在每个站点的主页上使用front.tpl模板 干杯。您可以在主题的template.php文件中添加theme\u preprocess\u页面函数,然后将模板名称添加到模板建议列表中

非常直截了当的问题:

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

我的网站分为两个部分,基本上是两个独立的网站,你可以在你的客户或企业主之间切换。所以我想在每个站点的主页上使用front.tpl模板


干杯。

您可以在主题的
template.php
文件中添加
theme\u preprocess\u页面
函数,然后将模板名称添加到模板建议列表中

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;
    // }
}
编辑

如果要按路径别名指定模板名称,可以编写如下代码:

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;
            }
        }
    }
}
如果没有此功能,默认情况下,您将有以下节点模板建议:

array(
    [0] => page__node
    [1] => page__node__%
    [2] => page__node__1
)
此函数将应用于节点的以下新模板建议。 带有
node/1
路径和
page/about
别名的示例节点:

array(
    [0] => page__node
    [1] => page__node__%
    [2] => page__node__1
    [3] => page__page
    [4] => page__page_about
)
因此,在这之后,您可以在页面中使用
page--page about.tpl.php

如果要将
page--front.tpl.php
应用于节点/15,则可以在此函数中添加If语句

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';
    }
}
这将为您提供以下模板建议:

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

最高的索引-最高的模板优先级。

您好,谢谢您的回复。我了解其中大部分内容,但如何调用此函数来激活特定页面。用于Expample node/14或URL alias pharmacy主页。Cheers您应该在主题的
template.php
文件中拥有此函数,该文件应该位于活动主题文件夹中。有关详细信息,请参阅我编辑的答案。太棒了,非常感谢您的解释。现在很清楚了。