How to remove the meta name=title element from Symfony 1.2 pages

February 2nd, 2009 | Tags: ,

Note: If you are using Symfony 1.0, follow these instructions instead.

A commenter from my prior post on this subject noted that those instructions only worked for Symfony 1.0 so I have updated the instructions to work for Symfony 1.2 (the current release as of this writing).

1. In your app’s lib dir, create a file named myWebResponse.class.php:

class myWebResponse extends sfWebResponse
{
   protected $title = null;

   /**
   * Retrieves title for the current web response.
   *
   * @return string Title
   */
   public function getTitle()
   {
      return $this->title;
   }

   /**
   * Sets title for the current web response.
   *
   * @param string Title name
   * @param boolean true, for escaping the title
   * @param boolean true, for allowing to overwrite the title
   */
   public function setTitle($title, $escape = true, $replace = true)
   {
      if (is_null($title))
      {
         unset($this->title);

         return;
      }

      // FIXME: If you use the i18n layer and escape the data here, it won't work
      // see include_metas() in AssetHelper
      if ($escape)
      {
         $title = htmlspecialchars($title, ENT_QUOTES, $this->options['charset']);
      }

      if ($replace || is_null($this->title))
      {
         $this->title = $title;
      }
   }
}

2. In your app’s lib dir, create a file named myViewConfigHandler.class.php:

class myViewConfigHandler extends sfViewConfigHandler
{
   /**
   * Adds http metas and metas statements to the data.
   *
   * @param string The view name
   *
   * @return string The PHP statement
   */
   protected function addHtmlHead($viewName = '')
   {
      $retVal = parent::addHtmlHead($viewName);

      $title = $this->getConfigValue('title', $viewName);

      if(!empty($title)) $retVal  .= "\n" . sprintf("  \$response->setTitle('%s', true, false);", str_replace('\'', '\\\'', preg_replace('/&(?=\w+;)/', '&', htmlspecialchars($title, ENT_QUOTES, sfConfig::get('sf_charset'))))) . "\n";

      return $retVal;
   }
}

3. In your app’s config dir, create a file named config_handlers.yml:

modules/*/config/view.yml:
  class:    myViewConfigHandler
  file:     %SF_APP_LIB_DIR%/myViewConfigHandler.class.php

Note: The reason the file setting is needed this time is detailed in this thread. The solution hack of using the file setting was taken from this forum post.

4. Modify the app’s factories.yml to use this response class instead of sfWebResponse

5. Clear the app’s Symfony cache

Note that you can still set the title in view.yml, additionally, this method allows you to set the meta title separately from the title element. For example:

default:
  http_metas:
    content-type: text/html

  metas:
    robots:       index, follow
    description:
    keywords:
    language:     en
    title:           This title will show up in the meta element with name=title

  title:          This title will show up in the title element
No comments yet.

TOP