Jump to content
xisto Community
Sign in to follow this  
Mordent

Creating A Template Engine General Advice?

Recommended Posts

So while in the process of revamping some of my websites I've decided to have a good shot at writing my own PHP/HTML template engine. For those who aren't sure what sort of thing I'm talking about, have a look at Smarty and think of a slightly toned down version of that. My question for you is what do you consider necessary in a simple template engine? On the basis that it'll be used by one-man (namely me, but I'll likely show people what I come up with if it works out to be any good), someone who has both knowledge of PHP and HTML etc., what sorts of features would you consider as "standard" in a template engine?

 

The main question I have for you is, really, the differentiation between a template and the code that uses the template. As far as I can tell there are two different schools of thought on how each should be done:

 

The first one suggests that all the programming bits (namely the PHP) should be separate from the HTML, whereas the second (and the one I think makes more sense) is that all the logic code should be kept separate from the display code. There's a subtle difference between the two:

 

What constitutes as "logic code" will almost certainly be entirely PHP, as are the "programming" bits, so the two methods agree in this respect. The difference occurs when it comes to the templates themselves: the first suggestion implies that all PHP (so <?php ?> tags) should be in the programming bit, with a much simpler variable implementation system for using the variables set by the coding section directly to show what's needed. The second suggestion makes a strong differentiation between the language and the purpose. Logic code and display code are two different things from PHP and HTML.

 

Overall, I certainly agree with the second of the two, although the main disadvantage of it is the requirement for the template designer to have some knowledge of PHP. Is that such a bad thing, though? You don't have to know much, perhaps a bit of variable manipulation and looping? There is, of course, the other disadvantage that by allowing the person who works with the templates access to directly run PHP code, so that could potentially post a security risk. That said, if you trust someone enough to upload templates to your site, surely that's not really a problem. Similarly, if you're to do most of the work yourself it's equally not a problem.

 

Has anyone attempted to make a template engine before? If so, how did it go? If not, do you use another one, and have you had any luck with it?

Share this post


Link to post
Share on other sites

Well, the template engine I'm using is a long time ago written logic structure by me, I didn't even use smarty.. even though smarty is much much smarter than my system, but what I'm using is Output buffering, I can catch the outputted html or parsed code, I can play with it using different functions and I have a callback function which will evaluate in the end of the code, I always have a template file or several files of the same template/site, where different parts of it is looking something like this: HTML code {description} HTML code {title} HTML code {section} and lets say I've got something like a variable $a = array('{section}' => $section); which is used for that purpose, of course it's not as easy as it can look here, you can add or append different values for it, also I'm usually using PHP code in the Template files, because using only HTML or {something} is a bit not so organized, usually the code in the Template files looks like:

if (something) then something or HTML else something HTML.. I call a function/method or I output something different for that situation in the Template, it's much easier to understand that in the template, rather than always adding a {something} and replacing with what should be done, for other people it might be hard to find where the hell is that {something} coming from.. For this kind of logic looking much more organised you should use OOP, create a class and use it or use more than one.. I don't know if it's the best method, but it works and it's really fast, I never get a very big page of code over 1 second, sometimes using 100 includes is slower than replacing parts of template..

I also created a Publishing, my current CMS can publish all the files into HTML only files logically, all using Output buffering and manipulating it, if you want to read more about OB, then have a look at: http://php.net/ob_start

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.