2009-10-28 79 views
2

我似乎無法動態設置在Kohana上構建的站點的$template變量。動態設置Kohana模板名稱

如果我延長Template_Controller類,我可以在模板名稱如下:

public $template = 'template_file_name'; 

但我不能把它像動態:

public $template = $this->setTemplate(); 

switch($var): 
    default: 
     public $template = 'filename'; 
     break; 
endswitch; 

在構造函數中使用$this->template更改$template變量會將T emplate_Controller莫名其妙:

Fatal error: Call to a member function render() on a non-object

我需要設置基於構造函數的變量組模板文件名,或者從外部庫拉。

任何想法如何使這成爲可能?

+0

我對Kohana一無所知,但setTemplate()實際上返回一個值嗎?你也應該在打開類(但在構造函數之外)之後聲明變量:'public $ template;'然後在構造函數中設置:'$ this-> template ='template';'。 – 2009-10-28 04:58:28

+0

嗯,應該有可能..!當你嘗試時會發生什麼?我用這種方式設置了很多屬性,包括ORM關係。儘管沒有嘗試使用廟名... – Cambiata 2009-10-28 04:59:42

+0

不,setTemplate沒有返回值 - 我只是試圖從內部函數返回一個動態值。 在構造函數中使用$ this->模板更改$ template變量會以某種方式打斷Template_Controller: 致命錯誤:調用成員函數對非對象的render() – jmccartie 2009-10-28 05:01:52

回答

5

我不喜歡這樣寫道:

public function action_myaction() 
{ 
    // template 
    $this->template = "template/overlay"; 
    parent::before(); 

    // display 
    $this->template->title = 'My Action'; 
    $this->template->content = View::factory('myaction') 
} 

更多信息這裏: http://www.workinprogress.ca/kohana32/

+0

謝謝!有用 ! – Winston 2013-07-29 13:39:04