在yii2中将标题页设置为h1

时间:2017-08-21 07:05:28

标签: php html yii yii2

在yii2中将标题页设置为h1?

在视图中:

$this->title = 'title' ;

和源代码:

<title>title</title>

2 个答案:

答案 0 :(得分:4)

在视图中,该值被分配给$ this-&gt;标题以供在head中使用,如果在用户视图中有用

<?php

   $this->title = $model->id;

?>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My Title  </title> <!-- this is for SEO -->
    ...
</head>
<body>
    <h1><?= $this->title ?> </h1> <!-- this is for user view -->

答案 1 :(得分:0)

设置标题变量。例如:

$this->title = $model->id;  //Set title

然后在视图或布局文件中使用title变量。例如:

Html::encode($this->title)

here

<?php
      # PHP code...
    use yii\helpers\Html;
    use app\assets\AppAsset;
    AppAsset::register($this);  //register an asset bundle
  ?>

<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <?= Html::csrfMetaTags() ?>  <!-- // CSRF -->

    <title><?= Html::encode($this->title) ?></title> <!-- for Page and Seo -->
    <?php $this->head() ?>

</head>
<body>
<?php $this->beginBody() ?>    
    <div class="wrap">
         <?= $content ?>  <!-- # view file:<h1> or...  #Html::encode($this->title)--> 
    </div>

<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>