php - yii2 how to replace customize meta tag from "view" with default meta tags in "layout" -


i want put default meta data description tag main template layout.

there many cases needed replace default meta tags description , keywords customize data in views.

i have tried:

$this->registermetatag 

in views need customize meta tags , have used in main layout.

i thought if use meta tags id or name replaced result shows duplicate meta tags e.g.

<meta id="main_index" name="description" content="my default content added layout.">   <meta id="main_index" name="description" content="my customized content added view"></head> 

what best way provide default description tag can customize views whenever call $this->registermetatag

i made self , since objective not repeat on each action default keywords or other meta tags, places replace use registermetatag in action.

  • first create own extended version of controller below

    class mycontroller extends controller {         public function beforeaction($event){     $this->view->title = yii::$app->params['pagetitle'];      \yii::$app->view->registermetatag(yii::$app->params['pagedefaultdescription'],"default_description");     \yii::$app->view->registermetatag(yii::$app->params['pagedefaultkeywords'],"default_keywords");     \yii::$app->view->registermetatag(yii::$app->params['pagedefaultauthor'],"default_author");      // open graph tags     \yii::$app->view->registermetatag(yii::$app->params['pagedefaultog_description'],"default_og_description");     \yii::$app->view->registermetatag(yii::$app->params['pagedefaultog_sitename'],"default_og_sitename");     \yii::$app->view->registermetatag(yii::$app->params['pagedefaultog_title'],"default_og_title");     \yii::$app->view->registermetatag(yii::$app->params['pagedefaultog_type'],"default_og_type");       return parent::beforeaction($event);     } }  
  • so customized keywords author etc meta tags can added config file of param default.

  • now new controller create extend mycontroller default above meta tags attached.

  • if need replace meta tag or customize in actions below.

     public function actionindex() {       \yii::$app->view->registermetatag([         'name' => 'description',         'content' => 'my customize description index.',         'id'=>"main_index"     ],"main_index"); //this replace default one.       return $this->render('index'); } 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -