Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Simplest Possible PHP Templating Engine

DZone's Guide to

Simplest Possible PHP Templating Engine

·
Free Resource
// 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 
 !
// 
// 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();
}
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}