Search code examples
phpmustachemustache.php

Mustache - How to build a Navigation/Link


I'm sorry for this stupid question but i don't get it!

I want to build a simple Website front-end like this Example with Mustache: http://detector.dmolsen.com/demo/mustache/

If I have a Website with just one Page - everything is fine: I create my Mustache Object ... and so on

// use .html instead of .mustache for default template extension
$options =  array('extension' => '.html');

// Template and Partial - Filesystem Loader
$mustache = new Mustache_Engine(array(
    'loader' => new Mustache_Loader_FilesystemLoader(__DIR__.'/views', $options),
    'partials_loader' => new Mustache_Loader_FilesystemLoader(__DIR__.'/views/partials/'.$GLOBALS['comparedDeviceInformation']['Device Class'], $options),
));

after this i render my index template:

// render index.html template
echo $mustache->render('index', $data);

This works perfectly fine! And now in this 'index.html' template file I wan't to link to another template e.g. home.html

<a href="?????">home</a>
how do i make such a dynamic rendering? It can't be the solution to have a php file for each page template?

And how do i handle URLs? i can't use the name of the templates? so how do i get a consistent URL sturcture for my Website

Thank you very much!


Solution

  • Mustache itself is just a templating engine… it is not concerned with what URLs you have, or how many PHP files you have, or anything other than changing a template into HTML. For all the other things, you have several options. The most straightforward is to have a single PHP file for every URL you want to handle. As you said, this isn't ideal :)

    I would suggest checking out a lightweight PHP framework. They handle routing (mapping URLs to rendered pages) and provide a lot more structure to your backend. Here is a list of some of the ones Mustache.php plays nice with. For your purposes, Silex or Slim would probably be a good fit.