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

[SOLVED] [FACEPALM] renderWith being overridden by default template SS3.1.10


Go to End


5 Posts   1700 Views

Avatar
zenmonkey

Community Member, 545 Posts

12 March 2015 at 3:10am

It seems the renderWith() in my init() is being overidden by the default tempalte for the page type. WHen I check ?showtempalte=1 that shows the correct template, but the one rendered is incorrect.

class Artwork_Controller extends Page_Controller {
	
	private static $allowed_actions = array (
	);

	public function init() {
		parent::init();
		
		$image = $this->OrderedImages()->first();
		
		if($image && $ratio = $image->getRatio()) {
			
			if($ratio > 1.2 ) {
				$this->renderWith("ArtworkWide");
			} elseif ($ratio < 0.8) {
				$this->renderWith("ArtworkNarrow");
			} else {
				$this->renderWith("Artwork");
			}
			
		}
		
	}
	
}

Avatar
catcherdev

Community Member, 9 Posts

12 March 2015 at 3:28am

I believe you'll have better luck overriding index() instead, as the parent's index is still having a chance to run w/ SS conventions.

Avatar
catcherdev

Community Member, 9 Posts

12 March 2015 at 3:33am

And you'll want to return $this->renderWith instead of just calling it (per Pyro @ irc).

Avatar
zenmonkey

Community Member, 545 Posts

12 March 2015 at 3:35am

Thanks. I really need more sleep. I shoudl have seen that

Avatar
Pyromanik

Community Member, 419 Posts

12 March 2015 at 3:43am

catcher is correct, this should not be done in init.
The default executed action is index

public function index(){ //executed (if present) on page access without action

And yes, also as mentioned renderWith returns data, it doesn't immediately pump to output.

But another caveat you should be aware of is template convention, and that renderWith technically takes an array to specify both 'main' AND 'Layout' templates.
At the moment if your template is templates/Layout/Artwork.ss (and derivatives) then this is all the page will render. ie. (presumably) no header, footer, maybe sidebar, etc.

so

$returnValue = $this->renderWith(['Artwork', 'Page']);