Mustache template engine for Slim Framework

Posted by admin on December 2, 2013

Nowadays i am trying to create a startup project for my website project. I selected Slim as Framework and Mustache as template engine however, when i searched usage of them together i really disappointed. SlimView out of date and there is sneaky ways for it but not real solution. So i write a wrapper for it.
It only uses render function. Other benefical functions of Mustache.php is not used. I will update class when i need these function.
Here is the SlimMustache class. Actucally i didnt test code below because i cuted and put a part of code here. Whatever, i think i will work otherwise it can work with a few correction.

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:

 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 errors :) I hope it helps.