【问题标题】:SilverStripe Overiding Template SS3.1.10SilverStripe 覆盖模板 SS3.1.10
【发布时间】:2015-03-11 14:06:31
【问题描述】:

我正在尝试根据图像大小覆盖页面模板。当我使用 ?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 块中,那么它会正确调用。但是在管道的最后一点被覆盖

【问题讨论】:

    标签: templates silverstripe


    【解决方案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; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多