Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Simple PHP Template Engine

Hello all, 

I've done quite a few small CMS-style projects for university and for random purposes. Each time, we ended up having to theme the project and it was always easier to do so by separating the HTML from the PHP. Fortunately, there are many template engines available, but some are very tedious to implement and are not suitable for small projects. 

Luckily, it's incredibly easy to do templates with no trouble whatsoever. This template engine is incredibly simple and can be added to your project in less than 5 minutes. I use it on every raw project I've ever done, and it's a great time-saver and makes our code much cleaner and easier to maintain. 

I've included the template_engine.php file with this post. It's licenced under the GPL 2, but if you need a different license, please PM me! 

1. Create a file called template_engine.php with the downloaded file attached here. Include (or require_once) this file into every file/page of your project (such as index.php, etc). You may want to use your require_once("template_engine.php") in your init or global.php instead, depending on how your project is structured.

2. Now, create a templates folder inside your project. This folder should be in your project root and will contain all HTML template files. 

3. Instantiate the template engine in each of these pages, or in your init file. Use the following: 

$templates = new template_engine(); 

4. Start setting template variables/tags in your code. These variables will be replaced with the values they are set to when the template engine outputs your page. For example, in your index.php, you could set variables like this: 

$templates->set("some_variable", "1234");
$templates->set("username", "Steve"); 

Now, the [@some_variable] tag will be replaced by "1234" by the template engine when it's ready to parse. Likewise, [@username] will be replaced with "Steve". Create as many variables as you like.

5. Create your index.html template in your templates folder. Use something like the following:

<title>My Index Page </title>

<h1> Hello, [@username]. Today's magic variable: [@some_variable] </h1> 

As you can see, we've used our new template variables directly in the template file. These variables will be replaced by their corresponding values on the fly by the template engine.

6. Parse the template and echo it. This will process all template tags and will send the resulting page to the browser. 

echo $template->parse("index"); 

And now, with a simple template engine and 5 minutes of implementation, you can abstract away all HTML from your PHP code. This allows you to completely refactor the style and hTML structure of your theme/page without changing any of your PHP files, and keeps the project much cleaner than it would be with interleaved HTML/PHP. 

Simple template engines such as these are perfect for small projects, university/college assignments, etc.

Hope this helps! Feel free to use it in your own projects as needed. Smile

More Information (click spoiler):
Guardian likes this post

Attached Files
.php   template_engine.php (Size: 2.58 KB / Downloads: 8) - Our next project...
Hello Darth, I'll test that right away, it's going to be fun, I'm looking forward to it, THANK YOU great idea
Darth-Apple likes this post
 [Image: autism4all.png]
[x] <= Drive in nail here for new display!
I was working on a project recently, and I realized this could be shrunk down quite a bit while keeping the exact same functionality. Reduced it by over 80% without even employing typical minification (I need to start coding a little more efficiently!) Finna

Lines: 17 (from 79)
Bytes: 408 (from 2.65KB)

//Template Engine. License: GPL v3

class template_engine {
	public $tags = array();

	public function set ($k,$v) {
	public function parse($tmpl) {
		return $this->parse_raw(file_get_contents("templates/$tmpl.html"));
	public function parse_raw($htm) {
		return strtr($htm, $this->tags);
	public function load($name) {
		$this->set($name, $this->parse($name));

And there you have it. The world's most lightweight template engine. Big Grin

(I say "engine," but it's essentially a wrapper for template bindings that stores variables, loads templates, and does string replacements without having to keep track of functions, directories, or variables directly. I've used it in several projects, and it's been a great time saver.)

Edit: Pushed this further... Cool
Guardian likes this post

Attached Files
.php   template_min.php (Size: 409 bytes / Downloads: 1) - Our next project...

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple PhpBB theme (getting my feet wet) Darth-Apple 3 328 October 23rd, 2020 at 7:05 PM
Last Post: Megan
  Good, simple blog software? Darth-Apple 0 2,311 June 19th, 2013 at 6:23 PM
Last Post: Darth-Apple

Users browsing this thread: 1 Guest(s)

Makestation Theme/Design Selector

Contact Us | Makestation | Return to Top | Lite (Archive) Mode | RSS Syndication 
Proudly powered by MyBB 1.8, © 2002-2020
Forum design by Makestation Team © 2020 - a modern day time capsule | Makestation Ajax Chat Hosting