Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use registerMetatag in Yii 2

Tags:

i'm trying to render meta tag on page header by use: In my View:

<?php
$this->registerMetaTag(['name' => 'keywords', 'content' => 'yii, framework, php']);
?>

and in my layout i put this code in head tag position:

<?php $this->head()?>

But it does not working. Anyone who have the same problem, can help me solve this problem. Thank you !!

like image 975
Chicken's King Avatar asked Apr 30 '15 17:04

Chicken's King


1 Answers

To define a meta tag through the registerMetaTag method, use the following code in your controller:

public function actionIndex()
{
    \Yii::$app->view->registerMetaTag([
        'name' => 'description',
        'content' => 'Description of the page...'
    ]);
    return $this->render('index');
}

and this markup in the layout:

<head>
    <?php $this->head() ?>
</head>

I've updated the basic demo application (from http://www.yiiframework.com/download/) that you can download here - https://www.dropbox.com/s/9rarzhtw65e5zo1/basic.zip?dl=0 and see this approach in action.

like image 110
Sergey Avatar answered Sep 23 '22 05:09

Sergey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!