Simple PHP template engine

Hello,

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?

EEEtc

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.


<?php

include(header.php);

include(someContent.php);

include(footer.php);

?>

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.

Thx

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.

template.php


class Template {

	private
	
	$_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);
		
		ob_start();
		$this->_compileTemplate($strFileContents);
		$strTemplateContents = ob_get_contents();
		
		ob_end_clean();
		
		return $strTemplateContents;
	
	}
	
	/*
	* Makes it possible for the template to return without
	* affecting anything. 
	* 
	* @param str file contents
	* @param obj module
	*/
	private function _compileTemplate($strFileContents) {
		extract($this->_arrTemplateData);
		eval('?>'.$strFileContents);
	}
	
}

index.php


include('template.php');

$tp = new Template();

$tp
->assign('var1',1)
->assign('var2',2)
->assign('var3',3)
->assign('var4',4);

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

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

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

layout.php


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title><?php echo $title; ?></title>
	<meta name="generator" content="BBEdit 9.6">
</head>
<body>

	<?php echo $yield; ?>

</body>
</html>

page.php


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