【问题标题】:Header/navbar/footer elements not importing into new page页眉/导航栏/页脚元素未导入新页面
【发布时间】:2015-10-16 23:53:53
【问题描述】:

我正在使用 UserFrosting,到目前为止,我已经能够将所有默认元素导入主页。但是,我现在添加了第二个页面,但是当我从主页复制以下代码时没有任何反应:

{% include 'common/components/head.html' %}
     <rest of code>
{% include 'common/components/main-nav.html' %}
     <rest of code>
{% include 'common/components/footer.html' %}   
{% include 'common/components/jumbotron-links.html' %}

然后我使用了以下 php 代码:

<?php include("../userfrosting/templates/common/components/head.html"); ?>

这似乎可行,但该页面仅显示在 head.html 文件中找到的此代码:

{% for item in includeCSS(page_group|default("common")) %} {% endfor %} {% for item in includeJSTop(page_group|default("common")) %} {% endfor %} 

这显然不是很有用!

当我将 home 和 page2.php 文件保存在同一个文件夹中(在 localhost/userfrosting/templates/common 中)时,我收到错误 404。当我将文件移动到默认的 UserFrosting 主页目录(即 home. html 文件实际上不在)在 localhost/public 中,我只得到上面的代码。

似乎我在这里遗漏了一些非常基本的东西,但希望能得到一些帮助。谢谢。

【问题讨论】:

  • 你不应该包含 head.html 而是你的实际页面 (page2.php ?)
  • 这不会导入我所在的页面(并尝试将页眉/页脚导入)吗?

标签: php header footer userfrosting


【解决方案1】:

您混淆了 PHP 文件和模板文件。 UserFrosting 使用front controller patternTwig 模板引擎。因此,您不需要为每个页面使用单独的 PHP 文件。相反,您应该为您的页面创建一个新的模板文件:

userfrosting/templates/common/page2.html

{% include 'common/components/head.html' %}
     // DO NOT use PHP here - just Twig!  See the Twig documentation: http://twig.sensiolabs.org/doc/templates.html
{% include 'common/components/main-nav.html' %}
     // More Twig
{% include 'common/components/jumbotron-links.html' %}
{% include 'common/components/footer.html' %}   

然后你需要用这个模板链接一个 URL。这是在控制器中完成的,public/index.php,例如这样:

// Miscellaneous pages
$app->get('/page2/?', function () use ($app) {    

    $app->render('common/page2.html', [
        'page' => [
            'author' =>         $app->site->author,
            'title' =>          "Page Deuce",
            'description' =>    "This is the second page, aight?",
            'alerts' =>         $app->alerts->getAndClearMessages()
        ]
    ]);          
});

我强烈建议您阅读有关添加新页面的教程:https://learn.userfrosting.com/building-pages

您还可以在此处了解有关 MVC、Slim 和 Twig 的更多信息:https://learn.userfrosting.com/basics/overview

如果您仍有问题,请随时加入chat

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多