Seamlessly use the template system with Kohana?

I plan to use Mustache templates with Kohana in my next project. So what I'm trying to do is to have Kohana seamlessly use Mustache when rendering the view. For example, I would have this file in my views folder:

myview.mustache

Then I can do in my application:

 $view = View::factory('myview'); echo $view->render(); 

Just like with a normal view. Kohana permits such things? If not, is there any way to implement it yourself using the module? (If so, what would be the best approach?)


PS: I looked at Kostache, but it uses custom syntax, which for me is the same as directly using Mustache PHP. I want to do this using the Kohana syntax.


Edit:

For information, here's how I did it based on @erisco's answer.

The full module is now available on GitHub: Kohana-Mustache

In APPPATH / classes / view.php :

 <?php defined('SYSPATH') or die('No direct script access.'); class View extends Kohana_View { public function set_filename($file) { $mustacheFile = Kohana::find_file('views', $file, 'mustache'); // If there no mustache file by that name, do the default: if ($mustacheFile === false) return Kohana_View::set_filename($file); $this->_file = $mustacheFile; return $this; } protected static function capture($kohana_view_filename, array $kohana_view_data) { $extension = pathinfo($kohana_view_filename, PATHINFO_EXTENSION); // If it not a mustache file, do the default: if ($extension != 'mustache') return Kohana_View::capture($kohana_view_filename, $kohana_view_data); $m = new Mustache; $fileContent = file_get_contents($kohana_view_filename); return $m->render($fileContent, Arr::merge(View::$_global_data, $kohana_view_data)); } } 
+4
source share
1 answer

Yes, you can. Since Kohana does some tricks with autoload, what they called "Cascading Filesystem", you can effectively override the functionality of the main classes. This is the same as Code Igniter if you are familiar.

In particular, this is the View :: factory method that you are referring to. Source

 public static function factory($file = NULL, array $data = NULL) { return new View($file, $data); } 

As you can see, this returns an instance of View . The View not initially defined, so PHP searches for it using autoload. This is when you can use the cascading file system function by specifying your own View class, which should be in the APPPATH/View.php , where APPPATH is the constant defined in index.php . Specific rules are defined here .

So, since we can define our own View class, we are good to go. In particular, we need to redefine View::capture , which is called $view->render() , to fix the inclusion of the template.

Check out the standard implementation to get an idea of โ€‹โ€‹what to do and what is available. I set out the general idea.

 class View { /** * Captures the output that is generated when a view is included. * The view data will be extracted to make local variables. This method * is static to prevent object scope resolution. * * $output = View::capture($file, $data); * * @param string filename * @param array variables * @return string */ protected static function capture($kohana_view_filename, array $kohana_view_data) { // there $basename = $kohana_view_filename; // assuming this is a mustache file, construct the full file path $mustachePath = $some_prefix . $basename . ".mustache"; if (is_file($mustachePath)) { // the template is a mustache template, so use whatever our custom // rendering technique is } else { // it is some other template, use the default parent::capture($basename, $kohana_view_data); } } } 
+5
source

Source: https://habr.com/ru/post/1397162/


All Articles