Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Cleaning up the output with HTMLTidy


Go to End


2 Posts   2533 Views

Avatar
John Silver

Community Member, 10 Posts

18 September 2009 at 9:14pm

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.

Avatar
John Silver

Community Member, 10 Posts

22 September 2009 at 4:39am

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.