2009-10-28 80 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