DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Michal has posted 1 posts at DZone. View Full User Profile

Simplest Possible PHP Templating Engine

11.28.2006
| 1929 views |
  • submit to reddit
        // This code takes a template name and an array of variables
// and parses them with a file
// 
// Example:
//   print template('hello', array('who'=>'world'));
//
// Template (/templates/hello.html)
//   Hello <?=$who?>!
// 
// Outputs:
//   Hello world!

define('DIR_TEMPLATES', dirname(__FILE__).'/templates');

function template($__name__, $__data__=array()) {
	extract($__data__);
	ob_start();
	require(DIR_TEMPLATES.'/'.$__name__.'.html');
	return ob_get_clean();
}