Micro templates for rapid web design prototyping and development in PHP

When you are developing a new web site, it’s common to start by writing a static HTML version that will later be converted to a theme in the CMS of your choice. It takes away the mental overhead of adhering to theming standards and lets you focus on the client-side technologies (HTML, CSS and JavaScript) unhindered.

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.

Live demo

Click to see a live running demo.

Download

Click to download the latest version, with examples.

Bitbucket

Click to see the Bitbucket repository

Usage

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:

<?=t('header')?>

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.

Prequisites

Short tags (short_open_tag) need to be enabled in your php.ini

Advertisements
Explore posts in the same categories: Computers, Programming, Technical solutions

Tags: , , , , , ,

You can comment below, or link to this permanent URL from your own site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s