Search code examples
phpmysqljsontemplatesmustache

Mustache PHP - How to load data from Database?


In Mustache data is loaded from. json static files and I can't understand how can I load data from a MySQL DB.

Example: currently the data contained in the "partials" are loaded from a .json file but, of course, whatever application needs to load data from a database in dynamic way (mysql in my case).

How can I do?

Thanks in advance!

Giovanni


Solution

  • Mustache PHP is only a templating engine. It's not an entire application with ready to go database bindings.

    You need to write an application that instantiates Moustache templates and gives the necessary data.

    $m = new Mustache_Engine;
    echo $m->render(
             'Hello {{planet}}',
             array('planet' => 'World!')
         ); // "Hello World!"
    

    The array with the planet=> 'World!' values is the data being passed to Moustache (in this case). You can load that data from a database and pass it to moustache as a variable.