Ok, this is the only documentation available right now, sorry about that. Will improve if there shows to be a need. For your hardcore hacking purposes, the code is pretty well commented, so that may help.
Setting it up
One of the goals of mplate is to encourage you to split your view code (”presentation logic”) from the rest of your program. This helps you keep things separate, and makes the part of your code where the HTML is produced uncluttered, easy to read, and easy to maintain. The part of an application where all the HTML is produced is often called the “view”.
As such, we’ll have (at least) two files: a normal PHP file, where you compute the data that you want to display, and an mplate template file (usually ends with “.tpl”), where you display that data. First, we make the PHP file, say, index.php.
<?php
require_once("Mplate.php");
$mplate = new Mplate();
We use the $mplate object for setting all kinds of settings, for sending data to the view, and finally for displaying the view. There are all kinds of settings, which I will describe elsewhere, but for now we show the two most important ones:
$mplate->settings["templateDir"] = dirname(__FILE__);
$mplate->settings["compileDir"] =
dirname(__FILE__) . "/compiled";
We tell mplate about two very important pieces of information: one is where all the template files can be found. All template files must be in (a subdirectory of) this directory. mplate compiles all template files into PHP files, and stores them in the “compile dir”. This way, the template code does not need to be evaluated more often than necessary. Make sure that this directory can be written to by the server.
Computing data
Now that mplate is set up, we compute data, and send it to the view through mplate. In our case, we want to show the world a list of our favourite actors and how friendly they are in person, but only when it’s after 13:37 in Moscow!
$actors = array();
//is it after 13:37 in Moscow?
$d = new DateTime("13:37",timezone_open('Europe/Moscow'));
if($d->format("U") < time())
{
//if so, compute favourite actors!
$first = array("Richard Dean", "David", "John", "Hulk");
$last = array("Anderson", "Hasselhoff", "Candy", "Hogan");
shuffle($first);
shuffle($last);
$a = array_combine($first, $last);
foreach($a as $k => $v)
{
$actors[] = array(
"name" => "$k $v",
"friendly" => abs(crc32("$k $v")%20)
);
}
}
//send actors to mplate
$mplate->vars['actors'] = $actors;
Yes, that’s a lot of complicated nonsense! But don’t worry, it’s totally irrelevant to the example. $mplate->vars is a simple public variable, that you can assign anything to (strings, arrays, objects, etcetera). It is recommended to first compute all data with local variables, and only in the end assign everything that you need in the view to $mplate->vars. This way, you can easily see the difference between intermediate variables, and the data that you actually want to display.
Finally, we tell mplate to display the view.
$mplate->display('actors.tpl');
Creating the view
Now that we computed a list with our favourite actors in it, we want to show the world! In the previous block of code, we ask mplate to display a file called actors.tpl. Well, here goes:
<html>
<head>
<title>My favourite actors</title>
</head>
<body>
<h1>Hello!</h1>
My favourite actors:
<ul>
{foreach $actors as $actor}
<li>{$actor.name} (friendliness: {$actor.friendly})</li>
{foreachelse}
<li><em>No cookie for you!</em></li>
{/foreach}
</ul>
</body>
</html>
As you see, mplate is just HTML code, interleaved with friendly looking operations. They look a bit like HTML, except that by the default settings, all mplate operations are enclosed by curly braces, like {this}.
Save the file, and point your browser to index.php; there you go!
Done!
You can probably also find the above example in the mplate distribution.
Now go read the rest of the docs!