Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change meta tags in zend framework layout

So I have some default meta tags on layout.phtml set using

$this->headTitle() and $this->headMeta()->appendName()

and is echoed at layout.phtml's header

My question is: How do I change those default meta tags from the view file such that they are replaced?

I tried using:

$this->headMeta()->appendName() or setName()

Instead of replacing the old default meta tags, it would create an entirely new meta tag. How can I replace them?

like image 322
kamikaze_pilot Avatar asked Mar 18 '11 08:03

kamikaze_pilot


2 Answers

I just tested this, and setName() should work:

<?php $this->headMeta()->setName('keywords', 'test'); ?>
<?php $this->headMeta()->setName('keywords', 'test'); ?>

Results in:

<meta name="keywords" content="another test" >  

While:

<?php $this->headMeta()->setName('keywords', 'test'); ?>
<?php $this->headMeta()->appendName('keywords', 'another test'); ?>

Results in:

<meta name="keywords" content="test" > 
<meta name="keywords" content="another test" > 
like image 147
Aron Rotteveel Avatar answered Oct 24 '22 21:10

Aron Rotteveel


I would recommend setting a view variable for the keywords. For example, in your bootstrap.php you could define a default keywords as follows:

protected function _initSetDefaultKeywords() {
     $view = $this->bootstrap('view')->getResource('view');        
     $view->keywords = 'default keywords';
}

In layout.phtml you would have then:

<?php echo $this->headMeta()->appendName('keywords', $this->keywords); ?>

Finally, in your views (or actions) you could change the meta keywords simply by changing the keywords view variable:

// in an action
$this->view->keywords = 'new kewords';
//or in a view
<?php $this->keywords = 'new kewords'; ?>
like image 40
Marcin Avatar answered Oct 24 '22 22:10

Marcin