SilverStripe Overiding Template SS3.1.10

时间:2015-03-11 14:06:31

标签: templates silverstripe

我试图根据图片大小覆盖页面模板。当我使用?showtemplate检查管道时,它表示正确的模板正在渲染,但事实上它是默认的。我的控制器是下面的

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

        }

    }

}

如果我将一个Debug注入它在页面上呈现的if块,那么它正确调用。但是在管道的最后一点被覆盖

1 个答案:

答案 0 :(得分:1)

ViewableData::renderWith()会返回一个HTMLText对象,您无法使用该对象。

SilverStripe不会像CodeIgniter那样将数据输出到输出中。

您正在寻找的是:

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