PHP Template Engine

I have been looking online for a tutorial to build a template engine. I know there are many engines that exist, like smarty, twig, and pattemplate, that could do exactly what I want, but I am looking to learn how to build one. I started with a template engine that added strings to an array and then displayed the array. Since then I built one using eval() (see below).

<// Define links & folders
define("ROOT_HTTP", "http://" . $_SERVER['HTTP_HOST'] . "/preprocessor");
define("TEMPLATE", "/template");

// Get the template file
$template = file_get_contents("template/template.php");

// Replace
$template = str_replace("<x Title x>", displayTitle(), $template);
$template = str_replace("<x Menu x>", displayMenu(), $template);
$template = str_replace("<x Content x>", displayContent(), $template);

$result = @eval("?>" . $template . "<?");

function displayMenu(){
    return "Link1<br />" . 
     "Link2<br />" . 
     "Link3<br />";

function displayTitle(){
     return "Site Title <?php echo date(\"m-d-y\", time()); ?>";

function displayContent(){
     return file_get_contents("content.php");

It works fairly well but its not what I am looking to achieve. I would like to build something that is like the Joomla template with tags like <jdoc:include type="component" />. I would also like it to be able to handle errors inline meaning that it will display the line number of an error or when I call echo "text" it displays text in the correct position inside the template.

How do I create something along those lines?

Answers sounds very similar and has good code organization. if extension of mentioned system does not suit the needs, it would at least work as good tutorial

First of all: Immediately forget the idea about using a TE with XML-like tags. Really, it may look nice on the first glance but only causes too much work in the end and is really limiting.

Secondly I obviously recommend you to use Twig. It is clean, fast, extensible and offers all the features you need.

And lastly: I have written a small tutorial how to write a simple but powerful TE in another Stackoverflow question. It is really simple but for smaller projects it may suffice.

I cannot agree with NikiC's point of view. XML is, although an old syntax, very powerful and brings a lot of advantages -- one of which is its similitude with properly written HTML.

There is nothing limiting in using an XML-based template syntax.

Besides, although Twig is, indeed, an excellent and famous project, it still lacks from a really good separation paradigm. It is still too dangerous and too easy to make mistakes from within the template and cause damages to the application as a whole.

Finally, the best template engine -- just as the best MVC framework -- is the one you feel really comfortable with. I recommend having a look at FigDice]1, which was inspired by PHPTal, but takes things a few steps further, with an exclusive approach by giving the Web Designer (integrator, html-ist, etc.) a central position with the project -- much more flexible than the Twig-like approach.

I would be happy to read some feedback. Thanks

Need Your Help

codeigniter passing array data from model to controller

php arrays codeigniter

I'm executing the following query in the get_distribuidor() function of my model:

Lambda “cannot be inferred from the usage”

c# linq lambda

I have the following dictionary declared:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.