使用 BEGINCONTENT() 和 ENDCONTENT() 设定 YII 的 LAYOUTS

Yii 的 views/layouts 是用来放置 layouts 的目录,在默认的情况下会有 main.php 和 column1.php 和 column2.php。

main.php 内容定义了,<head> 以及 page header 和 footer 等。 column1.php 和 column2.php 是使用 main.php 的网页布局,但修改内容的部份。

例如,我们有个 layout 叫做 mylayout.php:

<?php $this->beginContent('//layouts/main'); ?>
<div>
    <?php echo $content ?>
</div>
<div class="sidebar">
    <ul>
        <li>option 1</li>
        <li>option 2</li>
    </ul>
</div>
<?php $this->endContent() ?>

beginContent(‘//layouts/main’) 表示以 main 为布局,在  beginContent() 和 endContent() 之间为 content 呈现的修改。 beginContent() 和 endContent() 之外的范围不建议加入 HTML,否则会在 main.php 内容的前面或后面(<html> 标签之前或 </html> 标签之后)。

在 controller 里可以这样使用:

public function actionMylayout() {
    $this->layout = 'mylayout';
    $this->render('//site/index');
}

表示以 mylayout.php 为布局,views/site/index.php 为内容。

原文地址:https://www.cnblogs.com/xuan52rock/p/4534158.html