How to change meta tags in zend framework layout

14,039

Solution 1

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'; ?>

Solution 2

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" > 

Solution 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");

Solution 4

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;

Solution 5

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?

Share:
14,039

Related videos on Youtube

kamikaze_pilot
Author by

kamikaze_pilot

Updated on June 04, 2022

Comments

  • kamikaze_pilot
    kamikaze_pilot almost 2 years

    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?

  • kamikaze_pilot
    kamikaze_pilot about 13 years
    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 overwrite
  • Rhys
    Rhys about 13 years
    @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.
  • kamikaze_pilot
    kamikaze_pilot about 13 years
    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?
  • Xerkus
    Xerkus about 13 years
    use bootstrap to set defaults. Or use action helper/plugin to do it dynamically.
  • kamikaze_pilot
    kamikaze_pilot about 13 years
    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 working
  • Marcin
    Marcin about 13 years
    How do you bootstrap your view? Is it different that at official zend quick start tutorial?
  • ucefkh
    ucefkh about 11 years
    This is a quality first Code Amazing tank you !

Related