Setting Kohana Template Name Dynamically

I am unable to set a dynamic variable for a $template

site built with Kohana.

If I extend the Template_Controller class, I can set the template name like this:

public $template = 'template_file_name';

      

But I cannot set it dynamically like this:

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

      

or

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

      

Changing the variable $template

using $this->template

in the constructor breaks the Template_Controller in some way:

Fatal error: Calling member function () on non-object

I need to set the name of the template file based on a variable set in the constructor, or perhaps pull from an external library.

Any ideas how to make this possible?

+2


source to share


2 answers


This link might have an answer:

http://stii.co.za/php/overriding-default-template-in-kohana-php/



just run your template constructor like this:

public function __construct()
    {
        $this->template = 'foobar';
        parent::__construct();
    }

      

+8


source


I do it like this:

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

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

      



More information here: http://www.workinprogress.ca/kohana32/

+5


source







All Articles