在 Drupal 7 中使用 front.tpl 作为另一个页面

发布于 2024-12-17 20:56:19 字数 238 浏览 2 评论 0原文

非常简单的问题:

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

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

干杯。

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.

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

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

发布评论

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

评论(1

依 靠 2024-12-24 20:56:19

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

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 应用于您的 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';
    }
}

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

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

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

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.

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