How to replace meta tag in Yii?

I know that I can register a new meta tag in Yii, and I know how to do it, but I need to

replace the default tag that I set, because when I am in the article, I want to insert

brief description of the article in the meta tag;

How to manage meta tags?

+5
source share
3 answers

If you are using the latest version, you can give the meta tag identifier.

->registerMetaTag('example', 'description', null, array(), 'mytagid');

Calling registerMetaTag again with the same identifier will overwrite it.

http://www.yiiframework.com/doc/api/1.1/CClientScript#registerMetaTag-detail

+9
source

You can set the meta tag on the page using:

Yii::app()->clientScript->registerMetaTag("This is my meta description", 'description');
Yii::app()->clientScript->registerMetaTag("These, are, my, keywords", 'keywords');

, , , , , (, $model - , meta_description - , -):

Yii::app()->clientScript->registerMetaTag($model->meta_description, 'description');

Yii

+7

You can try the following:

1) In 'components / Controller.php' :

public $metaDescription;
public $metaKeywords;

public function getMetaDescription() {
    if(!$this->metaDescription)
        return Yii::app()->settings->getValue('meta_description'); //return default description
    return $this->metaDescription;
}

public function getMetaKeywords() {
    if(!$this->metaKeywords)
        return Yii::app()->settings->getValue('meta_keywords'); //return default keywords   
    return $this->metaKeywords; 
}

2) In the layout of main.php :

...
Yii::app()->clientScript->registerMetaTag($this->getMetaDescription(), 'description');
Yii::app()->clientScript->registerMetaTag($this->getMetaKeywords(), 'keywords');
...

3) In other layouts :

...
// If you don't do that, the description and keywords will be default for this page.
$this->metaDescription = 'Your description here';
$this->metaKeywords = 'your, keywords, here';
...

Please note that Yii :: app () → settings-> getValue ('meta_description') and Yii :: app () → settings-> getValue ('meta_keywords') are my default values, which are taken from the database.

+2
source

All Articles