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.

Template Questions /

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

Overriding layout


Go to End


2 Posts   1563 Views

Avatar
Eurisko

Community Member, 27 Posts

13 October 2015 at 3:11am

Edited: 13/10/2015 3:14am

Hi

I would like to minify all my html at render time. I've managed to do it for the header, footer and my forms. But any content that's in layout ss files I can't reach.

I've tried to create a method "layout" in my Page_Controller but it has sporadic results. Sometimes it returns a blank page, sometimes it gets in an infinite loop.

	    public function layout()
	    {
	    	$arrLayouts = array(
                      str_replace( "_Controller", "", get_class( $this ) ),
                      'Page'
                );
	    	return $this->minifyOutput( $this->renderWith( $arrLayouts ) );
	    }

I have a super simple function in my Page_Controller that I want to run the html through

	    function minifyOutput($buffer)
		{
			$search = array(
				'/\>[^\S ]+/s',
				'/[^\S ]+\</s',
				'/(\s)+/s'
			);
			$replace = array(
				'>',
				'<',
				'\\1'
			);

			$buffer = preg_replace($search, $replace, $buffer);

    		return $buffer;
		}

All I'd like to do is minify my pages. Am I looking in the wrong place? Is it even possible to override layout successfully?

Thank

Avatar
martimiz

Forum Moderator, 1391 Posts

24 October 2015 at 10:27am

Edited: 24/10/2015 10:28am

I don't think you can use a Layout() function like that. However you should be able to do something like this from within the Page_Controller index() function instead.

public function index() {
    return $this->minifyOutput($this->renderWith(array($this->Classname, 'Page')));
}

(Didn't test this though)