Nowadays I am trying to develop for my side project. I selected Slim as Framework and Mustache as template engine, However when I searched usage of them together, I am really disappointed. SlimView out of date(!) and there is sneaky ways for work it but not real solutions. So I write a wrapper for it.
It only uses render function. Here is the SlimMustache class.

class SlimMustache extends \Slim\View
{
	static private $mustache;
	public function __construct($temp_path){
		
		$this->data = new \Slim\Helper\Set();
		
		$this->mustache = new \Mustache_Engine(array(
			'partials_loader' => new Mustache_Loader_FilesystemLoader($temp_path.'/partials')
		));
		
		
	}
    public function render($temp_file)
    {
		
		$templatePathname = $this->getTemplatePathname($temp_file).'.mustache';
		
        if (!is_file($templatePathname))
            throw new \RuntimeException("View cannot render `$temp_file` because the template does not exist");
            
        return $this->mustache->render(file_get_contents($templatePathname), $this->data->all());
    }
}
	

Example usage: Slim setup file:

<?php

require 'vendor/autoload.php';
include('class/SlimMustache.php');


$app = new \Slim\Slim(array(
	'debug' => true,
	'mode' => 'development',
	'templates.path' => 'path',
	'view'=> new SlimMustache('path')
));

$app->get('/', function ()  use (&$app) {
	
	$data=array(
		'text'=>'Hey'
	);
	echo $app->render('deneme',$data);
});

$app->run();

?>

deneme.mustache

{{text}} you!

It is my first English blog post sorry for grammer mistakes :) I hope it helps.

  • php