Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Build your own PHP template engine

Wednesday, December 26th, 2012 by Servage

If you’re familiar with the Model-View-Controller (MVC) concept, then you know of the importance of separating layout from logic in your code. Most PHP frameworks are working with this basic principle, thus the implementation may differ a lot. Some frameworks have their own template engines, some use third-party engines – like Smarty.

Template Engines

Smarty is one of many available clever template engines. It is open-source and comes with a whole lot of features. However, like most template engines available, it may be too big of a mouthfull – or simply put: overkill – for your purposes. Most smalle development teams are in charge of everything alltogether, and trust each other well enought, so every developer can access and manipulate every level of the web application. Strictly separating layout from logic here may be inefficient, therefore only separating files is enough. I.e. you want different files for controllers and views, but you don’t have to enfore “no PHP code in the template” etc. In such cases there are a few engines available which allow for PHP code in templates. You could however, also simply create your own.


You can load and manipulate the content of rendered PHP scripts faily easily with PHP object:

$content = ob_get_clean();

If you place this – or a smiliar logic – somewhere in your controller process, then you can quickly get an own view renderer. This is very useful for simple custom PHP frameworks.


Build your own PHP template engine, 3.0 out of 5 based on 5 ratings
Categories: Tips & Tricks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.