Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "filter title"

AutoSet the title of the page

It's so long to set the title page per page and i forgot it frequently. With this filter it's the end of no titled pages.

Copy into lib/filter/sfAutoMetaFilter.class.php :

<?php
/**
 * Manage MetaTags 
 * @author C├ędric Lombardot <cedric.lombardot@spyrit.net>
 */
 
class sfAutoMetaFilter extends sfFilter{
    public function execute ($filterChain)
      {
        $filterChain->execute();
        /*
         * Recherche le h1 pour mettre le title
         */
 
 
        $html=$this->getContext()->getResponse()->getContent(); 
        $title=$this->get_title($html);
        $h1s=$this->get_h1($html);
        if($h1s[1]==0){
            $h2s=$this->get_h2($html);
            if($h2s[1]!=0){
                $this->getContext()->getResponse()->setContent($this->set_title($html,(($title)?($title.' - ' ):'').strip_tags($h2s[0][0])));
            }
        }else{
            $this->getContext()->getResponse()->setContent($this->set_title($html,(($title)?($title.' - ' ):'').strip_tags($h1s[0][0])));
        }
 
 
 
 
      }
 
      // retrieve all h1 tags
        protected function get_h1($html){
            $h1tags = preg_match_all("/(<h1.*>)(.*)(<\/h1>)/isxmU",$html,$patterns);
            $res = array();
            array_push($res,$patterns[2]);
            array_push($res,count($patterns[2]));
            return $res;
        } 
        protected function get_h2($html){
            $h2tags = preg_match_all("/(<h2.*>)(.*)(<\/h2>)/isxmU",$html,$patterns);
            $res = array();
            array_push($res,$patterns[2]);
            array_push($res,count($patterns[2]));
            return $res;
        } 
        protected function get_title($html){
            $title = preg_match("/(<title>)(.*)(<\/title>)/isxmU",$html,$patterns);
            return isset($patterns[2])?$patterns[2]:'';
        } 
        protected function set_title($html,$title){
            return preg_replace("/(<title>)(.*)(<\/title>)/isxmU",'<title>'.$title.'</title>',$html);
        } 
}
?>
 

And in your filters.yml

autometa: 
  class: sfAutoMetaFilter
 
by cedric lombardot on 2010-04-29, tagged filter  metas  title 
(2 comments)