Page 2 of 2

Posted: Thu May 13, 2004 8:32 pm
by McGruff
Templates aren't exactly mandatory but they are very important. They allow you to separate formatting (html etc) from the php code which defines all the unformatted, dynamic content.

This makes a script output independent (just write some new templates) and in most cases makes the scripts much more readable since, if you weren't using templates in the first place, it's odds on that you haven't kept business logic separate from presentation.

With templates, printing a client page is as simple as defining a bunch of vars in a php script, then including a template file containing embedded echo calls.

One day you discover a need to print a series of rows, looping through a database result set or something. Now you need to make a template function to perform the iteration: instead of an embedded echo call you have an embedded fn call. Perhaps the best way to do this, if you are happy with OOP, is to define the collection as an iterator in the php script, then use a decorator to print it in the template.

As the months pass by, more and more fns are added to your template fn library. One day you wake up and find you've created your own "template engine" in php.

If you haven't used templates before, I'd recommend this as the way to start.

Posted: Fri May 14, 2004 9:04 am
by scisoft
I have been cinsidering to use templates a couple of times, but was always afraid of the overhead. Started with Microsoft VB a couple of years ago, and only after learning some C++ realised how foolish the approach "lets the processor handle the overhead" is. So I am quite afraid of making the same error again.

Posted: Fri May 14, 2004 11:05 am
by dave420
I wrote my own templating class, and since then I've not looked back. Even for the most simple pages, a quick template can be the best thing. It can take care of including the right headers/footers, looping table rows, etc. You just have to set up an array, pass it to the template, and render.

PHP has come far enough for us to not mix PHP and HTML. I know you can, but as Chris Rock says - "you can drive your car with your feet, but it doesn't make it a good idea". We have the tools to seperate the two very different parts of PHP scripts (client & server), and even for small web pages, I can't see why it shouldn't be adopted. After all, it's not uncommon for small web pages to grow :)

I'm not having a go at anyone, or their ideas/beliefs. I've put a lot of thought into this. I understand everyone's point on the subject, and I'm still to be convinced otherwise. Templates are to the display what object-oriented code is to logic. As far as I'm concerned, there's no going back :)

Posted: Fri May 14, 2004 11:06 am
by magicrobotmonkey
hey can you give a rough outline of the class - like vars and methods, just so i can get some ideas?

Posted: Fri May 14, 2004 11:15 am
by patrikG
This one might be interesting for you: http://wiki.devnetwork.net/index.php/TemplatesTutorial

Posted: Fri May 14, 2004 11:27 am
by dave420
Mine has very basic language constructs available within the actual template itself.

The whole thing works on a template class, which you can construct an object from. That template object can then be given a template to use. You load the object up with data (in the form of arrays, if you want loops or collections of data). Once loaded, you run the render() method, which returns the template with all the logic executed and variables inserted where asked. It supports includes (ie you can include another template, logic an' all, into your template at a specific location), straight variable-replacement, boolean variable checking ("does $var evaluate to true?" etc.) and loops through arrays of data, basic mathematical checking (such as modulus, essential for breaking table rows at x cells across, and great for alternating row colours). It supports its own variable scope, as well as its own global variables (so you can access "global" variables from within an array loop, for example).

You make the constructs in the template like this (A loop):

%%*students%%%
%%%count%%% - %%%name%%%<br>
%%%students*%%

Which will loop through the "students" variable, outputting the line in the middle (which has "count" replaced by the iteration count, and "name" by $students["name"] for the current student.

I found this class is pretty easy to use. It's lightweight, yet I've managed to do all sorts of stuff with it (I use it for pretty much everything I do, even dynamic .m3u generation. With basic modifications, it could even be binary-safe, which would be really cool).

Posted: Fri May 14, 2004 11:57 am
by magicrobotmonkey
When you give it a template to use, What's that look like? I mean, you design a layout in html or whatever and then give it to it? So to design the template you simply have to know what data it is going to be "loaded" with?

I'm already very familiar with OOP, so I know the ideas behind building a class, I'm just tryinh to sort out how the template object is going to work. I'm also trying to figure out how I will integrate it with a session object. I think now the session object will instansiate the template object, giving it the appropriate dat to display whereever the user's at in the session. I'm just unsure of exactly how to do this.

For instance, I built a site for a client which emulates a paper form they were previously using. Its a four page form, so I do it the same way in the app to make it an easy transition for their clients. So the client logs in, verifies information, fills out page one, fills out page two etc. Currently, I have one file which keeps track of the clients "state" So it determines which page the client was on and what action he took therefore what page to display. It also deals with grabbing the post informatin. The part I don't like is that to display a page I have (say for page one) 1.spt, 1.bdy, and 1.chg (for javascript (html head), body, and pst data handler) So I don't like this. I mean I guess I could do in a dbase since these files are just text, but I want to design it better.

The thing is, is that each page is very different. I mean they have the same header and footer, but the content changes a lot so I don't know how to template them.

Posted: Fri May 14, 2004 12:11 pm
by xisle
Definitely use template classes on a case-by-case basis, I too have written my own as well as used a couple of decent open source apps.
(this is decent http://templatepower.codocad.com )
If you are preparing data for multiple outgoing feeds ie. html, wap, raw,
xml it makes a ton of sense as each output will need a different template
for formatting. It will cut your work load and provide a better product.

A biiiig problem that I run into is the fact that I must code both the script and the templates because the designers tend to shy
away from anything that isn't straight html or css. So, the projects become twice as lengthy to seperate the script from the display format. Now, if you have savvy designers that can adhere to template scripting regiments ie XSLT you are set!

Posted: Fri May 14, 2004 12:49 pm
by McGruff
scisoft wrote:I have been cinsidering to use templates a couple of times, but was always afraid of the overhead. Started with Microsoft VB a couple of years ago, and only after learning some C++ realised how foolish the approach "lets the processor handle the overhead" is. So I am quite afraid of making the same error again.
You have to try very, very hard to produce an inefficient script. In 99% of cases, alleged performance gains are in Hubble telescope territory rather than significant, real world improvements.

Embedding php echo calls and simple fns in html templates makes no difference compared to the spaghetti code alternative - but everything is now much easier to read (and a non-programmer can edit the templates with a wsywig editor). It's basically the same script, except that you're including an html file/s rather than sprinkling the html through the code.

You don't have to use a full-blown template engine and custom tags, but they are also a valid option which can allow for some nice tricks like adding dummy content for a template fn. This can be used to give the wsiwyg designers something to play with, but is replaced by the template fn output on the live page.