Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
The question is a bit hard to read, but the content is interesting. –  Florian Margaine Jul 31 '13 at 14:06
    
I think that trying to write less I couldn't explain very well. Do you think I should edit a little bit @FlorianMargaine? –  user1620696 Jul 31 '13 at 14:09
    
I don't really know what you should rewrite, but yes a little more space and/or clearer would certainly make the question better. –  Florian Margaine Jul 31 '13 at 14:14

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer

The idea behind using templates (and a template engine) is that you separate the (dynamic) content producing code from the code that is responsible for the visual appearance of the page. To make this separation successful, the application code should not be aware the fact that title and meta tags go in the HTML <head> element, while the rest of the content goes into the <body> element. In fact, the application should not even care if the templates produce HTML code or something completely different.

To facilitate the separation between application and template, the template engine should provide a mechanism where the application can provide replacement content without knowing where the placeholder for that replacement will occur in the templates. This also makes it possible to completely restructure the templates without any change to the application code.

share|improve this answer

I cannot give you a specific code example for your problem but the general direction for your problem. If you use a template engine (self programmed or not), it should have the ability to parse templates based on method calls and the ability to assign variables (look for Smarty).

Example:

public function display($templateName, $sendHeaders = true) {
   $this->compile($templateName); // compiles the template language into php script

   if ($sendHeaders) //send Headers

   include($this->getCompiledFilename($templateName));
   exit;
}

public function assign($name, $value) {
    $this->v[$name] = $value;
}

That being said you just use variables in the template for meta tags and the controller for the displayed page will first assign the variables and then display the template (via the template engine's methods).

An example for a template (you can combine multiple templates) would be:

{include file='header'}
{if $var1}
    <p>This is incredible.</p>
{/if}
{include file='footer'}

I hope that I helped you.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.