Code snippets for symfony 1.x

Navigation

How to remove meta tags

You may find yourself wanting to remove default meta tags for specific modules within your application. This isn't possible through view.yml or module.yml, as we can only add or replace meta tags. We could add a removeMeta() method and call it from the module's pre- or postExecute() method, but as the default metas are added after action execution, that wouldn't work either.

The solution is to extend the sfWebResponse class, overriding the getMetas() method. This allows us to filter out unwanted tags without affecting special behaviour, e.g. for the title tag.

class myWebResponse extends sfWebResponse
{
  public function getMetas()
  {
    $meta_tags = $this->parameter_holder->getAll('helper/asset/auto/meta');
    if ($this->getContext()->getModuleName() == 'special_module' && array_key_exists('bad_meta', $meta_tags)) {
      unset($meta_tags['bad_meta']);
    }
    return $meta_tags;
  }
}
by James McGlinn on 2006-06-17, tagged metadata  response 

Comments on this snippet

gravatar icon
#1 zzen on 2006-06-20 at 03:02

This is possible in the current version of symfony with stylesheets and javascripts using [-abc] See http://www.symfony-project.com/trac/changeset/1451

Maybe somebody should port the same functionality from the addHtmlAsset function to the addHtmlHead. That is, if we're sure nobody is ever going to need a meta value starting with "-".