PHP: A new kind of template engine

February 21, 2010

I have been developing a custom Content Management System (CMS) for Blam Designs and though there are several really good templating engines out there for PHP (such as Smarty) I find them usually bulky. Then there is also the need to learn the templating language as well. Though the templating languages are simple, it is one more thing for a designer to worry about knowing that pulls them away from their designs.

With that in mind I set out to find or create a new kind of templating system that works in a way similar to the JavaScript DOM, but works with PHP. The beauty of this sort of a system lies in the fact that a designer can create a layout with all the filler content and dummy text they can imagine and they do not have to remove any of it, or add any special template tags before uploading their snazzy layout to a web server. Simply add a normal HTML id attribute to whatever it is the designer wants to place content in and the template engine will automagically take care of the rest for you!

For example:

Lorem ipsum solet dolomar

Might be replaced with:

Hello World! I am some content

Sounds pretty cool doesn’t it? I wound up creating my own PHP classes to handle this for me. I ran across a library called PHP Simple HTML DOM Parser (SHDP) which does all the heavy lifting for me. My class it basically a wrapper object that makes it easier to deal with the SHDP, in addition to providing some extra functionality. Using my class it is possible to replace the entire contents of a tag (such as <title>), replace by the element id (such as <span id=”myid”>), or through the use of “special” tags. The “special” tags I call special because they do not fit inside the rest of the template paradigm. A special tag works in much the same manner as a tag from a normal template system like Smarty would. Tags are created using curly braces and look similar to {{my_tag}}. I needed to keep these in in the case that some special piece of code needs to be dynamically added to the template, but is not an HTML tag or id. This happens with things such as file paths.

Though I may not be fully done tweaking the new template engine it is fully operational and I am going to provide a few code example and a download of my class with SHDP packaged with it. Eventually I would like to setup a new site dedicated to this template engine, and would like to fully re-write SHDP so that the template system is one complete unit, not relying on any external libraries.

HTML File:

	Template Engine Demo

This is inside the contents div
This is inside the d2 div

Replace the contents of a tag:

$t->setTag('title', 'This is a new title');

Replace the contents of an element by id:

$t->setById('contents', 'Hello World! I am new content');

Append the contents of an element by id:

$t->setById('d2', ' I am appended content', 'a');

Set a special tag

$t->setSpecialTag('special_tag', 'I am special!');

Final Output:

	This is a new title

Hello World! I am new content
This is inside the d2 div I am appended content
I am special!

Please be sure to template_engine_demo the template engine to see all the working examples.

If you use this template engine on your site please drop me a comment below.

Enjoy 😀

Leave a Reply

Your email address will not be published. Required fields are marked *