I'm planning to use Mustache templates along with Kohana in my next project. So what I'm trying to do is to make Kohana seamlessly use Mustache whenever rendering a 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 I would do with a regular view. Does Kohana allow this kind of thing? If not, is there any way I could implement it myself using a module? (If so, what would be the best approach?)
PS: I had a look at Kostache but it uses a custom syntax, which for me is the same as using Mustache PHP directly. I'm looking to do it using Kohana's syntax.
Edit:
For information, this is how I ended up doing 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's 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's 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));
}
}