thezenmonkey
thezenmonkey

Reputation: 189

SilverStripe Overiding Template SS3.1.10

I'm trying to override the template of page depending on image size. When I check the pipeline with ?showtemplate it says teh correct template is rendering but fact it's the default. My controller is bellow

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");
            }

        }

    }

}

If I inject a Debug into the if block it renders on page, so it's calling correctly. But being overidden at the last point in the pipeline

Upvotes: 1

Views: 49

Answers (1)

agal
agal

Reputation: 79

ViewableData::renderWith() returns an HTMLText object, which you're doing nothing with.

SilverStripe does not pump data through to output willy-nilly like CodeIgniter did.

What you're looking for is:

public function index() { //this is important - do not perform this in init!

    $image = $this->OrderedImages()->First();
    $ratio = $image->Ratio;

    if($ratio > 1.2) $layoutTemplate = 'ArtworkWide';
    if($ratio < 0.8) $layoutTemplate = 'ArtworkNarrow';

    //the array/second element is redundant if your template is a 'main' template, not a 'Layout' one.
    return $image ? $this->renderWith([$layoutTemplate, Page]) : $this; 
}

Upvotes: 1

Related Questions