Jump to content

Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  yordan : (14 April 2014 - 05:28 PM) By The Way, This Could Be An Interesting Subject For A Topic, What About Posting This Question? Let's See If Other People Have The Same Feeling Concerning Bootlists!
@  yordan : (13 April 2014 - 09:36 AM) Boot Order : Cd, [Usb,] Hard Drive :D
@  yordan : (11 April 2014 - 07:23 PM) I Simply Let The Bios Do That
@  Ritesh : (11 April 2014 - 10:23 AM) Is It Possible To Launch Fedora Live Cd Or Installation Disk From Hard Drive On Windows Platform Using Grub Mbr File.
@  Ritesh : (11 April 2014 - 10:21 AM) No U Are Not.. Btw.. I Have Question For You.
@  yordan : (10 April 2014 - 08:02 AM) You Are Partially Right.
I Was Not.
Nevertheless, I Am Again :)
@  Ritesh : (09 April 2014 - 07:33 PM) :P
@  Ritesh : (09 April 2014 - 07:33 PM) I Think U R Not..
@  yordan : (09 April 2014 - 09:28 AM) I'm The Master Of The Shoutbox!
@  yordan : (05 April 2014 - 10:32 PM) He-He
@  Ritesh : (04 April 2014 - 06:59 PM) Ha Ha Ha ....
@  yordan : (04 April 2014 - 11:15 AM) Welcome Back, Starscream!
@  yordan : (03 April 2014 - 02:31 PM) And I Hope That He Will Come Back Soon :)
@  yordan : (01 April 2014 - 02:53 PM) Nice, Ritesh Came, I'm Not Home Alone Today.
@  Ritesh : (01 April 2014 - 08:51 AM) Oh!!! Poor Dear Yordan..
@  yordan : (31 March 2014 - 10:02 AM) I'm A Poor Lonesome Cow-Boy
@  yordan : (27 March 2014 - 02:22 PM) He Is Unpatient Due To His Patients!
@  Ritesh : (27 March 2014 - 10:46 AM) :(
@  Ritesh : (27 March 2014 - 10:46 AM) He Is Busy With His Patients.
@  yordan : (26 March 2014 - 08:12 PM) Ahsani, Where Are You?

- - - - -

Creating A Template Engine General Advice?

1 reply to this topic

#1 Mordent


    Premium Member

  • [HOSTED]
  • 271 posts

Posted 13 November 2008 - 08:04 PM

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?

#2 Quatrux


    the Q

  • [HOSTED]
  • 1,669 posts
  • Gender:Male
  • Location:Lithuania, Vilnius
  • Interests:PHP, MySQL, Oracle, PL/SQL, HTML, CSS, Javascript, jQuery, C# Computers, Alternative OS, Amiga, MorphOS, Beer, Friends, Linux, KDE..
  • myCENTs:30.76

Posted 14 November 2008 - 03:05 PM

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://www.php.net/ob_start

Reply to this topic


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users