代码之家  ›  专栏  ›  技术社区  ›  Ionut Flavius Pogacian

如何替换Yii中的元标签?

  •  5
  • Ionut Flavius Pogacian  · 技术社区  · 13 年前

    我知道我可以在Yii中注册一个新的元标签,我知道如何做,但我需要

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

    元标签中文章的简短描述;

    如何管理元标签?

    3 回复  |  直到 13 年前
        1
  •  9
  •   Alex    13 年前

    如果你使用的是最新版本,你可以给metatag一个id。

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

    使用相同的id再次调用registerMetaTag将覆盖它。

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

        2
  •  7
  •   Brett Gregson    13 年前

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

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

    这可以在Controller或视图中设置,显然,根据您查询文章的方式,您可以使内容部分动态(假设 $model 是你选择的文章和 meta_description 是存储元描述的模型属性):

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

    关于 Yii site can be found here

        3
  •  2
  •   Footniko    12 年前

    你可以试试这个:

    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; 
    }
    

    2) 在您的main.php布局中 :

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

    3) 在其他布局中 :

    ...
    // 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()->设置->getValue('meta_description')和Yii::app()->设置->getValue('meta_keywords')是我从DB中获取的默认值。