【问题标题】:How to create layout that use header and footer partials如何创建使用页眉​​和页脚部分的布局
【发布时间】:2015-08-15 23:40:32
【问题描述】:

我有关于视图和布局的指南,我已经用谷歌搜索了如何做到这一点,但我仍然无法使其工作。问题来了:

我想要 2column.php 和 3column.php 布局,两者都使用 _header.php 和 _footer.php 部分。

2columns.php 布局示例:

render _header.php

$content and some other extra code

render _footer.php

无论我做什么,我都无法让它发挥作用。有人可以向我发布如何实现这一目标的真实简单示例吗?谢谢

请注意答案:

使用:

<?php $this->beginContent('@app/views/layouts/header.php'); ?>
    <!-- You may need to put some content here -->
<?php $this->endContent(); ?>

对我没有帮助...不知道该怎么办,我不能让它做我需要的。

【问题讨论】:

  • 您是否尝试过使用 PHP include 'file.php' 包含页眉和页脚文件?
  • 我希望有一些 Yii2 方式。

标签: php layout view yii2


【解决方案1】:

你应该尝试一下:

<?php $this->beginPage() ?>
<?= $this->render('@app/views/layouts/header', $_params_) ?>

<!-- main content -->

<?= $this->render('@app/views/layouts/footer', $_params_) ?>
<?php $this->endPage() ?>

别忘了在标题视图中使用以下内容:

<head>
    <?= Html::csrfMetaTags() ?>
    <?php $this->head() ?>
    ...
</head>

阅读更多:

【讨论】:

  • 终于...这行得通。我的页眉中有&lt;?php $this-&gt;beginPage() ?&gt;,页脚中有&lt;?php $this-&gt;endPage() ?&gt;,所以我必须先删除它们并将它们放在主布局文件中。非常感谢!
  • 通常您也可以将视图指定为绝对视图://layouts/header。这也应该有效。
  • @soju 你好!为什么不使用renderFile() 而不是render()
【解决方案2】:

您可以使用以下内容来呈现页眉和页脚:

echo \Yii::$app->view->renderFile('@app/views/layouts/footer.php'); 

【讨论】:

  • 我已经尝试过了,我的页面搞砸了,没有应用 css。 Css 应该在 head 部分,但如果我像这样阅读标题,我不会在那里。
猜你喜欢
  • 2020-05-08
  • 2019-03-14
  • 2012-01-19
  • 2011-08-25
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 2011-03-12
  • 2013-04-21
相关资源
最近更新 更多