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

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

问题描述

相当直接的问题:



我在drupal 7站点中使用了page-front.tpl和page.tpl模板页面。但是,我想在另一个页面上使用page-front.tpl。这是可能的,或者我需要创建另一个.tpl页面来做到这一点。



我正在处理的网站分为两部分,它基本上是两个独立的网站,您可以在不管您是消费者还是业主之间进行翻转。所以我想使用front.tpl模板为每个站点的主页。



干杯。

解决方案

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

  function mytheme_preprocess_page(& $ vars){
//你可以执行不同的if语句
// if(){...
$ template ='page__front'; //你应该用下划线替换虚线符号
$ vars ['theme_hook_suggestions'] [] = $ template;
//}
}

编辑 / p>

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

  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 路径和页面/约别名的示例节点

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

因此,您可以使用页面 - page-about.tpl.php 为您的页面。



如果要将页面 - front.tpl.php 应用于让我们说 node / 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';
}
}

这将给你以下模板建议: p>

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

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


Pretty straight forward question:

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.

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.

Cheers.

解决方案

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

EDIT

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
)

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
)

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

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天全站免登陆