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

Simple PHP Template Engine

#1
Hello all, 

EDIT: See the GitHub version. A new version is available with significant improvements. https://github.com/Darth-Apple/simple-php-templates

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:

<head> 
<title>My Index Page </title>
</head>

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


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: 32)
Saturn-Moon.com - Our next project...
Reply
#2
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!
Reply
#3
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)

<?php 
//Template Engine. License: GPL v3

class template_engine {
	public $tags = array();

	public function set ($k,$v) {
		$this->tags["[@$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: 20)
Saturn-Moon.com - Our next project...
Reply
#4
I've been doing some work to make this a more fully-featured template engine. Its goal is still to be incredibly lightweight, but it now supports:

Loops:

This is perhaps one of the most useful features that is present in established engines such as Twig or Blade. It allows you to load a list of posts, for example, by "looping" the same HTML code for each post. This can be applied for pretty much any kind of list imagineable. This engine has native support for simple for loops, such as:

[@loop: people as person]
Person's name: [@person:name] <br />
Person's age: [@person: age]<br />
[/loop]

HOST CODE (PHP) 
$templates->set_array("people", $people_array); // Sets the "people" variable for the looping engine

This was implemented with a BBcode-like Regex parser that supports nesting via recursive callbacks. It first looks for loops and creates a recursive "tree" - and it evaluates the inner loops first to determine if it needs to execute them. It works its way back up the "tree", finishing them one-by-one as to support multiple consecutive loops or complex nested structures. The "inner-loop first" structure is a bit unorthodox as far as parsing is concerned, but but was a concession made to support more complex structures using plain regex.

Currently, only "FOR" loops are supported. No plans are in place to implement "while" loops.

Language strings

This engine will now parse and manage basic language strings. It keeps track of a "locale" that looks into a specific folder for the core language file. You may load additional language files for specific pages as needed, which saves performance by not loading every single language string for every page.

$templates->lang_load("users"); // Loads all language strings in Languages/[locale]/users.lang.php

TEMPLATE: 

<div class="user">
[@lang:username] - Insert stuff here <br />
</div>


IF/ELSE statements

These features are almost universally present in other established template engines, and allow conditions to be evaluated directly in the template. This results in cleaner code and less PHP evaluation work.

Before IF statement: <br />

[@if: $logged_in == true]
  - Display logged out link
[/if]
[@else]
  - Display logged in link
[/else]

Developing support for these proved to be unusually tricky. Recursive Regex callbacks were used for implementation (an unusual choice for this type of job). Pure Posix-compliant regex actually cannot parse XML-like languages in this manner (tokenizers and specialized parse trees are used instead). However, PHP uses the PCRE regex engine (one of the most powerful regex engines in existence) that has the necessary features to make this possible.

Unfortunately, it has drawbacks. Performance being one of them, among strict syntax requirements built around the requirements of the regex. It is also easy to break with mismatched tags. However, with properly formatted syntax, nested if/elseif/else blocks are fully supported. Most of the StackOverflow examples lacked support for nesting, so this implementation is a unique Regex implementation.

Standard Template Variables
What would a template engine be without template variables? Thankfully, we aren't using straight PHP, nor are we using eval. This reduces the risk of PHP code injection and improves security over using flat PHP templates.

$templates->set("Username", "Steve"); 

TEMPLATE: 
<strong>Username: </strong> [@username] 

Right now, it's sizing out around 10KB of code or so. It will likely expand a little as I debug things, but the goal is to keep it fully contained within about 15KB or so. If it proves to be stable enough to be used in real world applications, I'll post the updated version on Github and will probably try to use it in a few projects of my own. I'll post some explanations on the code as a reference on how to do syntax parsing with Regex as well. Big Grin

Edit: It now compiles templates to pure PHP for native performance - After doing all of the work using regex to write a full interpreter for the basic template conditionals/loops, I wrote a compiler to do the exact same thing. It writes pure PHP template cache files and saves them for future use. The template engine will now run at native speeds, and has an interpreter as a fallback. Unfortunately, this exploded it to about 20KB in size. Going to trim this and see what can be done. Finna
Saturn-Moon.com - Our next project...
Reply
#5
I have the new version up on Github (in beta). It's bare-bones, approximately 6.55KB, and is fully functional for basic templating applications. A mere 166 lines in whole, it is likely one of the smallest template engines in existence! Big Grin

It's also lightning fast. It compiles the templates down to pure PHP files and caches them, so there is near-zero overhead with parsing the templates and rendering them. This proved to be much more stable and much easier than the previous interpreter-based approach.

https://github.com/Darth-Apple/simple-php-templates

Still working out a few rough edges and bugs, but as of yet, it's managed to beat the 10KB goal, and it's even implemented more features than originally planned. I'm looking forward to using it for future projects and university assignments. Finna

Edit: Just like last time, we tried our minification experiment to squash it into the smallest size possible. Click the spoiler to see stats.

Saturn-Moon.com - Our next project...
Reply
#6
Thank you, and test it right away :-) Big Grin
Darth-Apple likes this post
 [Image: autism4all.png]
[x] <= Drive in nail here for new display!
Reply
#7
I've done some more work refining this, and am now using it in a homegrown project of my own. It's actually managed to get the attention of a few folks I know from various places, so I'm glad to see that it seems to be in use so far. Tongue

Information Page: https://github.com/Darth-Apple/simple-ph...lates/wiki

Features implemented:
  • Template inheritance (extensions)
    • Allows a parent page to contain basic layout (header, footer, etc) with a child template containing page-specific data.
    • Allows blocks to be injected into a parent template at predefined locations
  • Listeners/Events (template hooks)
    • These allow functions to be called at predefined locations within the templates.
    • These are very useful for menus or sidebar blocks, where the content may need to be appended dynamically within PHP.
  • If/else blocks
  • Language packs/locale strings
  • Basic substitution variables
  • Template loops (PHP foreach)
  • Autoescape variables (XSS protection)
  • Template backreferences (include another template within a template)
  • Compiles to vanilla PHP (for native performance)

Footprint:

Lines: 231
Size: 8.3KB
Files: 1

Though it has grown, its lightweight spirit has remained a top priority during the development. It still weighs in under 10KB and is fully functional, containing many of the same features offered by the larger, more popular engines!
Saturn-Moon.com - Our next project...
Reply
#8
Wink  PERFEKT !!
Darth-Apple likes this post
 [Image: autism4all.png]
[x] <= Drive in nail here for new display!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple PhpBB theme (getting my feet wet) Darth-Apple 3 619 October 23rd, 2020 at 7:05 PM
Last Post: Megan
  Good, simple blog software? Darth-Apple 0 2,517 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-2021
Forum design by Makestation Team © 2020
Saturn-Moon.com - a modern day time capsule | Makestation Ajax Chat Hosting