mrreza
mrreza

Reputation: 71

set title page to h1 in yii2

set title page to h1 in yii2?

in view :

$this->title = 'title' ;

and source code :

<title>title</title>

Upvotes: 2

Views: 1149

Answers (2)

user206
user206

Reputation: 1105

Setting the title variable. example:

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

Then use the title variable in the view or layout file. example:

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

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133370

In view the value is assigned to $this->title for the use in head and if useful in user view

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

Upvotes: 4

Related Questions