How to change meta tags in zend framework layout
Asked Answered
C

5

9

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?

Cheerful answered 18/3, 2011 at 8:3 Comment(0)
P
11

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'; ?>
Pentaprism answered 18/3, 2011 at 11:20 Comment(2)
I tried doing that but then it returns error: Fatal error: Uncaught exception 'Zend_Application_Bootstrap_Exception' with message 'Resource matching "view" not found' and when i added resources.view[] = "" in application.ini, my view helpers suddenly stop workingCheerful
How do you bootstrap your view? Is it different that at official zend quick start tutorial?Pentaprism
T
13

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" > 
Tali answered 18/3, 2011 at 8:35 Comment(4)
it didn't seem to work....my first setname was set in the layout and it was meant to be a default meta tags for all the pages...the other setname was set in the view file but it didn't overwriteCheerful
@kamikaze pilot, when calling setName() inside your layout, be sure that you do it before actually echo'ing it. The placeholder concept does not work in the layout: it works from the views.Tali
yea i did and when i do that the setname in the view doesn't replace the one from the layout...is there a better way to set default meta tags other than this?Cheerful
use bootstrap to set defaults. Or use action helper/plugin to do it dynamically.Valene
P
11

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'; ?>
Pentaprism answered 18/3, 2011 at 11:20 Comment(2)
I tried doing that but then it returns error: Fatal error: Uncaught exception 'Zend_Application_Bootstrap_Exception' with message 'Resource matching "view" not found' and when i added resources.view[] = "" in application.ini, my view helpers suddenly stop workingCheerful
How do you bootstrap your view? Is it different that at official zend quick start tutorial?Pentaprism
B
3

Here's what worked for me. In the layout file you have to make sure that the meta tags are echoed. It's empty at the stage but you will be marking where the meta tags will be outputted. This only disadvantage with this method would be that there doesn't seem to be a way to have a default meta tag so you will have to add the meta tag in each view file.

In the layout file

<?php echo $this->headMeta(); ?>

In the view .phtml file

$this->headMeta("test description text", "description");
Beckner answered 7/3, 2014 at 13:19 Comment(0)
A
1

keywords and description generating without controller/action

register 2 plugin in bootstrap

    // register navigation, $view->navigation()->setContainer( new Zend_Navigation( $navigationArray ) );
    $controller = Zend_Controller_Front::getInstance();
    $controller->registerPlugin(new App_Controller_Plugin_PrepareNavigation());

    $controller->registerPlugin(new App_Controller_Plugin_SetMeta());

Meta plugin could look like:

    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $view = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view;
        $activePage = $view->navigation()->findOneBy('active', true);

        $view->headTitle($activePage->title);
        $view->headMeta()->appendName('keywords', $activePage->keywords );
        $view->headMeta()->appendName('description', $activePage->description );
        $view->pageHeader = $activePage->pageHeader;
    }

navigationArray would be than:

          'pages' => array(
            array( 'label'  => 'introduction',
                   'controller' => 'controller',
                   'action' => 'introduction',
                   'route'  => 'controlleraction',
                   'pageHeader' => 'h1 or something',
                   'title' => 'used as meta title'
                   'keywords' => 'meta keywords'
                   'description' => 'meta desc',

than you can simple call (from layout/view) print $this->pageHeader;

Accelerator answered 20/4, 2011 at 19:34 Comment(0)
D
1

How about:

$keywords = 'php,zend,framework';
$this->view->headMeta($keywords,'keywords','name',array(),'SET');

I found this, which works for me, on Best practice to place meta tags, links and styles in zend framework?

Digitalin answered 30/1, 2012 at 12:55 Comment(1)
This is a quality first Code Amazing tank you !Ikeikebana

© 2022 - 2024 — McMap. All rights reserved.