Good day!
Not so long ago, I began my acquaintance with the frameworks in principle and immediately faced the question of how to organize a simple change of templates so that you can add, say, another folder with templates in the templates folder and change templates in the future without serious consequences. But in Kohana 3 (unfortunately, I can’t say anything about other frameworks, since I worked with them only superficially) firstly, all View-files are in the views folder (after all, templates are simpler and clearer to the simple user), and second, there is no mechanism for selecting the desired View file from the Views folder (except for connecting it directly). Well - now I want to tell how to implement it. The used version of the framework is 3.0.7, the latest at the moment.
Let's start.
The ability to use the Templates folder instead of Views
First, we will look at the Kohana_View file, which is inherited by the dummy class View. The class is quite simple, but we are specifically interested in the set_filename () function - it is there that it is “hard” to indicate where to look for the View file:
public function set_filename($file)
{
if (($path = Kohana::find_file('views', $file)) === FALSE)
{
throw new Kohana_View_Exception('The requested view :file could not be found', array(
':file' => $file,
));
}
// Store the file path locally
$this->_file = $path;
return $this;
}
Now let's create our Template class, inheriting Kohana_View and slightly changing the set_filename function:
public function set_filename($file)
{
$folder = 'templates/'.self::get_name();
if (($path = Kohana::find_file($folder, $file)) === FALSE)
{
throw new Kohana_View_Exception('The requested layer :file could not be found', array(
':file' => $file,
));
}
// Store the file path locally
$this->_file = $path;
return $this;
}
Here, a reference to a static function that does not exist in the parent class — self :: get_name () —is flashed; let's write it now. Its task will be to get from configs in the database (this is just my specific example, here everyone can change it for themselves) the name of the template installed on the site.
public static function get_name()
{
$template = DB::select('value')
->from('config')
->where('key', '=', 'template')
->limit(1)
->execute()
->current();
return $template['value'];
}
Now, if necessary, we can easily find out the name of the current template by referring to Template :: get_name (). There was an idea at the beginning to put the name into a static variable, but practice has shown that such an approach is sometimes inapplicable (although with proper skill it is easy to pervert).
Further down the list we have a factory - no frills:
public static function factory($file = NULL, array $data = NULL)
{
return new Template($file, $data);
}
That's all - the Template file is done, you can save it as application / classes / template.php
')
Writing our Controller_Template
Now let's write our controller Controller_Template, based on the Kohana_Controller_Template framework offered by:
class Controller_Template extends Controller{
/**
* @var string template layer
*/
public $layer = 'index';
/**
* @var boolean auto renders template
*/
public $auto_render = TRUE;
/**
* Loads the template object (Template instance)
*/
public function before()
{
if($this->auto_render === TRUE)
{
$this->template = Template::factory($this->layer);
return parent::before();
}
/**
* Assigns the template as the request response.
*/
public function after()
{
if($this->auto_render === TRUE)
{
$this->request->response = $this->template;
}
return parent::after();
}
}
Now, let's say, when writing an authorization controller that will use a separate layer templates / [template_name] /auth.php, it will be enough to inherit it from Controller_Template and add a line
public $ layer = 'auth';
That's all.
I would be glad if my article will help someone. Also I will be glad to any healthy criticism, questions, comments ... In general, I wrote for them. :)
Thank you for attention.