如何替换Yii中的meta标记?

如何替换Yii中的meta标记?,yii,meta-tags,Yii,Meta Tags,我知道我可以在Yii中注册一个新的meta标签,我知道怎么做,但我需要这样做 替换我设置的默认标记,因为当我在文章上时,我想插入 meta标签中文章的简短描述 如何管理元标记?您可以使用以下方法设置每页的元标记: Yii::app()->clientScript->registerMetaTag("This is my meta description", 'description'); Yii::app()->clientScript->registerMetaTag(

我知道我可以在Yii中注册一个新的meta标签,我知道怎么做,但我需要这样做

替换我设置的默认标记,因为当我在文章上时,我想插入

meta标签中文章的简短描述


如何管理元标记?

您可以使用以下方法设置每页的元标记:

Yii::app()->clientScript->registerMetaTag("This is my meta description", 'description');
Yii::app()->clientScript->registerMetaTag("These, are, my, keywords", 'keywords');
这可以在控制器或视图中设置,并且显然取决于您查询文章的方式,您可以使内容部分像这样动态(假设
$model
是您选择的文章,
meta\u description
是存储元描述的模型属性):


如果您使用的是最新版本,则可以为metatag提供id

->registerMetaTag('example', 'description', null, array(), 'mytagid');
使用相同的id再次调用registerMetaTag将覆盖它

您可以尝试以下方法:

1)在“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; 
}
...
Yii::app()->clientScript->registerMetaTag($this->getMetaDescription(), 'description');
Yii::app()->clientScript->registerMetaTag($this->getMetaKeywords(), 'keywords');
...
...
// 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';
...
2)在main.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; 
}
...
Yii::app()->clientScript->registerMetaTag($this->getMetaDescription(), 'description');
Yii::app()->clientScript->registerMetaTag($this->getMetaKeywords(), 'keywords');
...
...
// 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';
...
3)在其他布局中

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; 
}
...
Yii::app()->clientScript->registerMetaTag($this->getMetaDescription(), 'description');
Yii::app()->clientScript->registerMetaTag($this->getMetaKeywords(), 'keywords');
...
...
// 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';
...

请注意,Yii::app()->settings->getValue('meta_description')和Yii::app()->settings->getValue('meta_keywords')是我从数据库获取的默认值。

我已经有了一个description meta标记;我想替换描述的内容;你说的替换是什么意思?加载页面后是否替换?我看你已经接受了答案,只是出于好奇问:)是的,用新内容替换默认元标记的内容我已经有了描述元标记;我想替换描述的内容;我上面的回答可以做到这一点。使用id将允许您覆盖任何现有描述;我们解决了这个问题,;我们的Yii不管理静态配偶标签;它只管理您使用其内部引擎生成的标记;