PHP – Creating a Custom PHP Template Engine

PHPtemplates

I've been developing a custom PHP template engine to suit just my needs and also to get a little more practice with PHP.

What I did was to create a Template class that simply receives as constructor parameter a path to a template file and a string that should be the placeholder for the contents of the pages. In that case, the template file is an HTML file with some placeholders written as [placeholder]. These will be filled with real content by another class. So for instance, [title] should be filled with the title, [content] with the content of the selected page and so on.

I've created also a Page class that receives as constructor parameter the directory of the pages and then has methods to set which name of file it should use (this is filled in as we like in the initialization, when working with friendly URL's the most common way I use this is setting as the first piece of the URL) and methods to set more data as pairs of placeholder string and real content.

My only problem is with how to deal with titles and meta tags. For each page there's a title and a collection of meta tags. My only way to deal with this was to create in different files one array with titles and one array with meta tags. Both of them receives as index the name of the page file. So for instance $titles["home"] should be the title of the home page and $meta_tags["home"] should be the html code for the meta tags of the home page.

The problem is that as soon as there are lots of pages this will have one unecessary cost of memory: the system would be loading for every requests all the titles and all meta tags, while it must load only the required one. I thought on using database, but it seems like killing one ant with an atomic bomb. So the next best thing I thought of was XML.

Now, what's the best solution for this kind of situation? Use XML, use a MySQL database, or some other method?

As I've said, I know that the recommended is that we use the template engines that are already out there since they are tested lots of times, are already stable and so on, but I'm struggling with those things as a way to get more practice developing with PHP.

Thanks very much in advance for the help. Also, I didn't think it was needed to post any code I've written, but if some of the code will help, please ask and I'll post it.

Best Answer

I'm not sure if I've understood your problem correctly or not, but that seems to me that Loading the data is your problem, not anything else related to your template engine, right?

If yes, my questions is, where are the other contents of your application? I mean where is the page content? Put the meta tags, titles, etc. just where you have the other data.

If you're storing your page content in the database, then add these meta tags (whatever) to your database, load them when you loading the page content and then just pass them to your template engine.

If you have static pages, then add something like these to each page, and later you can grab it and pass it to your template engine:

<?php
/* Sample.php */
$title = 'Sample Page';
$meta = 'whatever';
?>
<!-- Page Content -->
<h3>Welcome!</h3>
...

P.S. BTW, such an array of titles and meta tags is not an issue for a real-world web server.

Related Topic