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)); } }
source share