Search code examples
phpjsontemplate-enginemustache.php

A Template engine written in PHP that use JSON format as data?


I need a Template engine written in PHP that use JSON file as it's data. The popular engine that I love it's template format was Mustache.php but what I understood from it's documentation, data for this engine is PHP classes that is not very pretty format.

What I need is something like this:

Data in JSON + {{A Pretty Template that use curly braces for objects}} = A clean HTML

And of course I want this engine to do the compiling on the server. Your comments are appreciated.


Solution

  • Use json_decode function to decode JSON data into PHP Objects, like:

    require '/path/to/mustache/src/Mustache/Autoloader.php';
    Mustache_Autoloader::register();
    
    $mustache = new Mustache_Engine();
    
    $tpl = $mustache->loadTemplate('foo'); // loads __DIR__.'/views/foo.mustache';
    echo $tpl->render(json_decode('my_data.json'));