Micro templates for rapid web design prototyping and development in PHP
When working on an early static version of a site, I often find that I do miss simple templating, especially for listings (be it news posts, sidebar banners or anything that needs to be “filled up” to make the page look as it would in production) – So I set out to look for a clean, simple and minimal way to introduce templating into static projects, using PHP.
I found this post on micro templates by Brad Touesnard that perfectly suited my needs, but felt that it was missing some polish and examples for people to fully utilize it. I wrote a couple of examples for common tasks such as displaying lists and nesting templates, and released it as a library that you can use in your projects to enable faster prototyping and development.
Download php-microtemplate and put it on a php-enabled server, navigate to the php-microtemplate folder in your browser and look at the examples.
If you want to use it from scratch, just include microtemplate.php at the top of any php file:
<?php include_once('microtemplate.php'); ?>
Add an empty templates folder, so the directory structure looks like this:
Now you can use the t() function to display templates (which are just PHP files) from your templates/ folder, for example this snipped will display the content of templates/header.php:
You may optionally pass a second parameter that will send a variable or array to the template (to be used as $v inside the template), and a third optional variable that can set the folder path where the function will look for templates, which is useful if you prefer a nested directory structure for your templates.
Short tags (short_open_tag) need to be enabled in your php.ini