yii2.0布局 layout

http://www.yiibai.com/yii2/yii_layouts.html
布局代表多个视图的公用部分。例如,页眉和页脚。 默认情况下,布局应存放在 views/layouts 文件夹中。
让我们看一下基本应用程序模板的主(main )布局 -
<?php
   /* @var $this \yii\web\View */
   /* @var $content string */
   use yii\helpers\Html;
   use yii\bootstrap\Nav;
   use yii\bootstrap\NavBar;
   use yii\widgets\Breadcrumbs;
   use app\assets\AppAsset;
   AppAsset::register($this);
?>

<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang = "<?= Yii::$app->language ?>">
   <head>
      <meta charset = "<?= Yii::$app->charset ?>">
      <meta name = "viewport" content = "width = device-width, initial-scale = 1">
      <?= Html::csrfMetaTags() ?>
      <title><?= Html::encode($this->title) ?></title>
      <?php $this->head() ?>
   </head>
	
   <body>
      <?php $this->beginBody() ?>
         <div class = "wrap">
            <?php
               NavBar::begin([
                  'brandLabel' => 'My Company',
                  'brandUrl' => Yii::$app->homeUrl,
                  'options' => [
                     'class' => 'navbar-inverse navbar-fixed-top',
                  ],
               ]);
					
               echo Nav::widget([
                  'options' => ['class' => 'navbar-nav navbar-right'],
                  'items' => [
                     ['label' => 'Home', 'url' => ['/site/index']],
                     ['label' => 'About', 'url' => ['/site/about']],
                     ['label' => 'Contact', 'url' => ['/site/contact']],
                     Yii::$app->user->isGuest ?
                        ['label' => 'Login', 'url' => ['/site/login']] :
                        [
                           'label' => 'Logout (' . Yii::$app->user->identity->username.')',
                           'url' => ['/site/logout'],
                           'linkOptions' => ['data-method' => 'post']
                        ],
                  ],
               ]);
					
               NavBar::end();
            ?>
            <div class = "container">
               <?= Breadcrumbs::widget([
                  'links' => isset($this->params['breadcrumbs']) ? $this>params
                     ['breadcrumbs'] : [],
               ]) ?>
               <?= $content ?>
            </div>
				
         </div>
			
         <footer class = "footer">
            <div class = "container">
               <p class = "pull-left">© My Company <?= date('Y') ?></p>
               <p class = "pull-right"><?= Yii::powered() ?></p>
            </div>
         </footer>
			
      <?php $this->endBody() ?>
   </body>
</html>
<?php $this->endPage() ?> 

这种布局是所有视图的通用页面生成HTML页面。$content 变量是视图渲染内容的结果。以下方法引发有关渲染过程事件,以便在其他地方注册的脚本和标签会被适当注入 -

  • head() − 应在头部分调用。产生一个占位符,将与定位于头部位置已注册的 HTML 来代替。

  • beginBody() − 应在 body 部分的开头调用。触发 EVENT_BEGIN_BODY 事件。产生将使用定位在 body 已注册的HTML,将替换占位符开始位置。

  • endBody() − 应在 body 结束部分被调用。触发 EVENT_END_BODY 事件。

     产生一个占位符,这将有针对性的在 body 的结束位置使用已注册的HTML来代替。

  • beginPage() − 应在布局的开头被调用。触发EVENT_BEGIN_PAGE 事件。

  • endPage() − 应在布局结束时调用。触发 EVENT_END_PAGE 事件。

创建布局

第1步 - 在目录 views/layouts 里面创建一个名为 newlayout.php 的文件并使用下面的代码。
<?php
   /* @var $this \yii\web\View */
   /* @var $content string */
   use yii\helpers\Html;
   use yii\bootstrap\Nav;
   use yii\bootstrap\NavBar;
   use yii\widgets\Breadcrumbs;
   use app\assets\AppAsset;
   AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang = "<?= Yii::$app->language ?>">
   <head>
      <meta charset = "<?= Yii::$app->charset ?>">
      <meta name = "viewport" content = "width = device-width, initial-scale = 1">
      <?= Html::csrfMetaTags() ?>
      <title><?= Html::encode($this->title) ?></title>
      <?php $this->head() ?>
   </head>

   <body>
      <?php $this->beginBody() ?>
         <div class = "wrap"> 
            <div class = "container">
               <?= $content ?>
            </div>
         </div>
			
         <footer class = "footer">
            <div class = "container">
               <p class = "pull-left">© My Company <?= date('Y') ?></p>
               <p class = "pull-right"><?= Yii::powered() ?></p>
            </div>
         </footer>
      <?php $this->endBody() ?>
   </body>
	
</html>
<?php $this->endPage() ?>
在这里,我们已经移除了顶部的菜单栏。
第2步 - 要将布局应用到SiteController中,需要将 $layout 属性添加到 SiteController 类。
<?php
   namespace app\controllers;
   use Yii;
   use yii\filters\AccessControl;
   use yii\web\Controller;
   use yii\filters\VerbFilter;
   use app\models\LoginForm;
   use app\models\ContactForm;
   class SiteController extends Controller {
      public $layout = "newlayout";
      /* other methods */
   }
?>
第3步 - 现在,如果到Web浏览器打开 SiteController 中的任何视图,如打开URL=> ,您将看到的布局已经发生了变化。
第4步- 要注册的各种 meta 标签,可以在内容视图中调用 yii\web\View::registerMetaTag() 函数。
第5步 - 修改 SiteController 的“About”视图 - views/site/about.php。
<?php
   /* @var $this yii\web\View */
   use yii\helpers\Html;
   $this->title = '关于我们';
   $this->params['breadcrumbs'][] = $this->title;
   $this->registerMetaTag(['name' => 'keywords', 'content' => 'yii, Yii教程, Yii视图,
      meta, 标签']);
   $this->registerMetaTag(['name' => 'description', 'content' => '这是一个页面的描述!'], 'description');
?>
<div class="site-about">
   <h1><?= Html::encode($this->title) ?></h1>
   <p>
      This is the About page. You may modify the following file to customize its content:
   </p>
   <code><?= __FILE__ ?></code>
</div> 

我们刚刚注册了两个 meta 标签 − keywords 和 description.

第6步 - 现在打开 http://localhost:8080/index.php?r=site/about ,你会发现在页面头部的 meta 标签内容如下面的屏幕截图。

视图触发几个事件 -
  • EVENT_BEGIN_BODY − 触发了布局通过调用 yii\web\View::beginBody()

  • EVENT_END_BODY − 触发了布局通过调用 yii\web\View::endBody()

  • EVENT_BEGIN_PAGE − 触发了布局通过调用 fyii\web\View::beginPage()

  • EVENT_END_PAGE − 触发了布局通过调用 yii\web\View::endPage()

  • EVENT_BEFORE_RENDER − 在控制器开始渲染一个文件时触发

  • EVENT_AFTER_RENDER − 渲染文件后触发

也可以对这些事件的响应内容插入到视图。
步骤7- 要显示当前的日期和时间在 SiteController 的 actionAbout 动作中,以下面的这种方式。
public function actionAbout() {
   Yii::$app->view->on(yii\web\View::EVENT_BEGIN_BODY, function () {
      echo date('Y-m-d H:i:s');
   });
   return $this->render('about');
}
第8步 - 在浏览器中访问URL=>  http://localhost:8080/index.php?r=site/about,将会看到下面的内容。

要点

为了使视图更易于管理,你应该 -
  • 将复杂的视图分成几个小块文件
  • 使用公用的HTML代码部分(页眉,页脚,菜单等)的布局
  • 使用小部件-widgets
视图应该 -
  • 包含HTML和简单的PHP代码格式化和呈现数据。
  • 无处理请求
  • 不修改模型属性
  • 不执行数据库查询

转载于:https://www.cnblogs.com/haiwei_sun/articles/5979812.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值