2015-06-02 104 views
5

我有關於視圖和佈局的紅色指南,我已經用Google搜索瞭如何做到這一點,但我仍然無法使其工作。這是問題:如何創建使用頁眉​​和頁腳部分的佈局

我想有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(); ?> 

不幫我......不知道怎麼用它做什麼,我不能讓它做什麼,我需要。

+0

您是否嘗試過使用PHP'包括「file.php''包括頁眉和頁腳文件? – jakecfc1992

+0

我希望有一些Yii2的方式。 – offline

回答

5

你應該簡單地嘗試:

<?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> 

瞭解更多:

+0

終於...這個工程。我在我的頁眉中使用了' beginPage()?>',在我的頁眉中使用' endPage()?>',所以我必須先將它們刪除並將它們放在主佈局文件中。非常感謝! – offline

+0

通常,您也可以將視圖指定爲絕對值:'// layouts/header'。這也應該起作用。 – Blizz

0

您可以使用以下方法來呈現頁眉和頁腳:

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

我已經嘗試過,並且弄亂了頁面,沒有將css應用到它。 Css應該在頭部,但如果我的讀者頭像這樣,我不明白。 – offline

相關問題