Organizing a simple template change using Kohana 3

    Good day!

    Not so long ago, I began my acquaintance with the frameworks in principle and immediately came across the question - how to organize a simple change of templates so that at any time it would be possible to add, say, to the templates folder one more folder with the template and change the templates painlessly in the future. 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 (all the same, templates are simpler and more understandable to a simple user), but secondly, 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 you how to implement this. The framework version used is 3.0.7, the latest at the moment.
    Let's start.

    Ability to use the Templates folder instead of Views


    First, look at the Kohana_View file, which is inherited by the View dummy class. The class is quite simple, but the set_filename () function is of particular interest to us - it is it’s “hard” to specify where to look for the View file: Now let's create our Template class, inheriting Kohana_View and slightly changing the set_filename function: Here, a call to the class that does n’t exist is the parent of the static function is self :: get_name () - let's write it now. Its task will be to obtain from the 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 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;
    }


    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;
    }


    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 contacting Template :: get_name (). At the beginning, the idea was to put the name in a static variable, but practice has shown that such an approach is sometimes not applicable (although it is easy to pervert with proper art).

    Further down the list we have a factory - no frills: That's all, actually - the Template file is finished, you can save it as application / classes / template.php
    public static function factory($file = NULL, array $data = NULL)
    {
    return new Template($file, $data);
    }



    Writing your Controller_Template


    Now let's write our Controller_Template controller, based on the proposed Kohana_Controller_Template framework: Now, let's say when writing an authorization controller that will use a separate layer templates / [template_name] /auth.php, it’s enough to inherit it from Controller_Template and add the line public $ layer = 'auth'; That, in fact, is all. I would be glad if my article helps someone. I will also be glad to any healthy criticism, questions, comments ... In general, I wrote for their sake. :) Thank you for your attention.
    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();
    }
    }









    Also popular now: