Jump to:

5453 Posts in 1674 Topics by 1198 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Cleaning up the output with HTMLTidy

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1332 Views
  • John Silver
    Avatar
    Community Member
    10 Posts

    Cleaning up the output with HTMLTidy Link to this post

    I'd like to clean the output of my templates using HTMLTidy.
    I tried several approaches but I just can't get it right. With the help of this recipe: http://doc.silverstripe.org/doku.php?id=recipes:syntax-highlighting I managed to clean single pieces of the output, but not the whole document.

    My idea would be to take the output from SSViewer::process(), run it through tidy->parseString and return it.
    I think I need to overwrite the call for ViewableData::renderWith() (not overwrite the function itself) with my own and insert my extension class to SSViewer there.

    Roughly like this:

    class SSViewerTidy extends SSViewer {
       public function process($item) {
          $tidy = new tidy;
          $tidy->parseString(parent::process($item));
          return tidy_get_output($tidy);
       }
    }

    somewhere else:

    renderWith(new SSViewerTidy($template))

    But I can't find where renderWith() is called when a request is made.
    Where is my "somewhere else"?

    Any help would be appreciated.

  • John Silver
    Avatar
    Community Member
    10 Posts

    Re: Cleaning up the output with HTMLTidy Link to this post

    Sooo.. I got it to work. It's not pretty, but maybe it will help others.

    Created a class SSViewerTidy:

    class SSViewerTidy extends SSViewer {
       public function process($item) {
          //return(parent::process($item));
          $config = array   (
           'indent-spaces' => 2,
           'wrap' => 160
          );
          $tidy = new tidy;
          $tidy->parseString(parent::process($item), $config, 'UTF8');
          return tidy_get_output($tidy);
       }
    }

    And added this to my Page_Controller class:

       function getViewer($action) {
          // Hard-coded templates
          if($this->templates[$action]) {
             $templates = $this->templates[$action];
          }   else if($this->templates['index']) {
             $templates = $this->templates['index'];
          }   else if($this->template) {
             $templates = $this->template;
          } else {
             // Add action-specific templates for inheritance chain
             $parentClass = $this->class;
             if($action && $action != 'index') {
                $parentClass = $this->class;
                while($parentClass != "Controller") {
                   $templates[] = strtok($parentClass,'_') . '_' . $action;
                   $parentClass = get_parent_class($parentClass);
                }
             }
             // Add controller templates for inheritance chain
             $parentClass = $this->class;
             while($parentClass != "Controller") {
                $templates[] = strtok($parentClass,'_');
                $parentClass = get_parent_class($parentClass);
             }

             // remove duplicates
             $templates = array_unique($templates);
          }
          return new SSViewerTidy($templates);
       }   

    The content is completely copied from Cotroller.php (which is extended by Page_controller) - only the last line is altered to use the new SSViewer.
    Yeah...ugly. I know.

    1332 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.