Simple PHP template engine


I am looking for the smallest simplest php templating engine. I want to use it for static websites (ie .html) so I can separate header from footer from content. So when I need to change the header I only change one page not several pages.

Can you guys/gals recommend any?


mmm… sounds like you would just need php by itself to accomplish this.

There are several out of the box templating engines that are pretty good. I personally like the simplicity and speed of Smarty, but there are literally hundreds of others that get the job done just fine.

but as I stated, for your situation of just separating header/footer/content then you would just be able to use include() for this. So you may not need a templating engine at all.

An example of a pure php solution would be something like this.






Smarty is really big… no no no I need a very small templating engine something like one or two files.

Take a look on this article:

Its old, but its a good one imo.

Keep in mind PHP by itself is a “template” engine so if you do not need all the restrictions template systems like Smarty add, why not make a simpler one similar to the one mentioned in the article.

Use this:

I like to use tidy all the time to make my code nicer and clear to read. The above “include” technique does not work when using tidy. Correct me guys if I am wrong.


Tidy will add your starting/ending html tags for you…
So you have to first make your template, use Tidy, and then cut it up in blocks.

There’s Twig, which is MUCH smaller than Smarty. I’ve used it myself, and I like it.

Twig is awesome, yet probably too much for this.

Twig is not a simple templating engine. It has couple dozen classess, multiple inheritances, interfaces, etc. It may be a great templating engine, but it’s not simple. You can browse the source code and see how many files and classes are involved:

Here is one that makes it easy to nest pages using buffering.


class Template {

	$_arrTemplateData = array();

	public function __construct() {
	* Assign a variable by name to scope of template
	* @param str variable name within template
	* @param mix variable value
	public function assign($strName,$mixValue) {
		$this->_arrTemplateData[$strName] = $mixValue;	
		return $this;
	* Get variable assigned to the template
	* @param str name
	* @return mix variable value
	public function getTemplateVars($strName) {
		return isset($this->_arrTemplateData[$strName])?$this->_arrTemplateData[$strName]:null;
	* Get contents of a Template
	* @param str template file path
	* @return str template contents
	public function fetch($strTemplateFile) {
		if(!file_exists($strTemplateFile)) {
			trigger_error("Template file $strTemplateFile doesn't exist.");
		$strFileContents = file_get_contents($strTemplateFile);
		$strTemplateContents = ob_get_contents();
		return $strTemplateContents;
	* Makes it possible for the template to return without
	* affecting anything. 
	* @param str file contents
	* @param obj module
	private function _compileTemplate($strFileContents) {



$tp = new Template();


$yield = $tp->fetch('page.php');

->assign('title','My Awesome site!')
->assign('yield', $yield );

echo $tp->fetch('layout.php');


	<title><?php echo $title; ?></title>
	<meta name="generator" content="BBEdit 9.6">

	<?php echo $yield; ?>



<p><?php echo $var1; ?></p>
<p><?php echo $var2; ?></p>
<p><?php echo $var3; ?></p>
<p><?php echo $var4; ?></p>