News:

The anti-spam plugins have stopped being effective. Registration is back to requiring approval. After registering, you must ALSO email me with your username, so that I can manually approve your account.

Main Menu

Writing a PHP Templating System

Started by Databits, September 14, 2005, 09:11:40 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Databits

I've decided to continue on writing the templating system for my site, as I progress I'll go ahead and post results here to help other people here who may wany to implement a similar system into their own site.

Currently hte system I'm working on can only do single level block element substitutions. If you are at all aware of PhpBB's template system, this is something I'm trying to implement and improve upon. Their system is capable of encapsulating arrays of data much further than single depth. I haven't quite figured out how I'm going to pull off something similar without incorperation a recursive algorithm, but I'm hoping I can.

Now if you're wondering what I mean by all this... take a forum for an example. Most forums are database driven, and if they use a template system, the template itself only defines what a single forum should look like. Thus, a single template would be used for each and every thread in a forum, or in the listing of the forums on the front page.

This is indeed an interesting concept. Since the forum is database driven, the database can control how many categories are on the front page, how many forums are in each category, etc... but it simply uses either a built in format or a template for each (which repeats with different data).

Now the way a forum template is usually set up is in data blocks. For instance, take the following code as an example:

Example Template Code:
Quote[START CAT]

 
 
[CAT.NAME]
[START CAT.FORUM]
   
     
     
   
[CAT.FORUM.NAME]
[CAT.FORUM.DESC]

    [END CAT.FORUM]
   

[END CAT]
Ok, so what's this mean?? I'll explain that in just a moment. But first let me set up what the database would contain. For this example I'll use the index of the xepher.net forums. So the data in the database would be something like this:

Quotecategories
the format here is " ID - Name "
  0 - Community
  1 - Xepher.net
  2 - Content

forums
the format here is " ID - Name - category ID "
  0 - General Chat - 0
  1 - Knowledge Trading Post - 0
  2 - Announcements - 1
  3 - Applications - 1
  4 - Technical Support - 1
  5 - Hosting Q&A - 1
  6 - Art - 2
  7 - Writing - 2
  8 - Web Design - 2
  9 - Misc. Content - 2
Now this should be somewhat understandable. It's basically stating that there are 3 categories: Community, Xepher.net, and Content. Then there are 10 total forums. The General Chat and Knowledge Trading Post forums are part of the Community category. The Announcements, Applications, Technical Support, and Hosting Q&A forums are all part of the Xepher.net category. So on and so forth.

So now that we have a (basic) database structure, how does this apply to the template? Well that's where the template system comes into play with the block level elements. See, in your script you would create a template object, then assign it a template file to use (assuming the one above), create the template structure using the database data, and parse the output to the web browser. Seem complex yet?

So let's say I make a template object and assign it the template file example above. Then as the script loops through the database it would create a structure using data from it. After loading our database data the template would have a structure something like this:

Quote[template_file_name] => Array (
            [CAT] => Array (
                    [NAME] => Community
                    [FORUM] => Array (
                            [NAME] => General Chat
                            [DESC] => Anything not directly related to the web-hosting service or otherwise covered under the other forums.
                    )
                    [FORUM] => Array (
                            [NAME] => Knowhow Trading Post
                            [DESC] => We all have different talents and skills. Here's where you can help each other out. Need to find some obscure mediaeval literature links? Maybe someone else here majored in it. Wonder what it means when your car goes "woga woga woga" down hills? Someone might know a bit about cars. Perhaps you can code, but need some graphics for your site. Well maybe there's an artist that needs some coding done. Help each other out!
                    )
            )
            [CAT] => Array (
                    [NAME] => Xepher.net
                    [FORUM] => Array (
                            [NAME] => Announcements
                            [DESC] => Important things I might need to tell you.
                    )
                    [FORUM] => Array (
                            [NAME] => Applications
                            [DESC] => Want a site here? Then this is the place to start. Please make sure to read the "Instructions" post before starting.
                    )
                    [FORUM] => Array (
                            [NAME] => Technical Support
                            [DESC] => It's broke? Can't figure out where that file should be? Ask here (but search first!) Support for hosting-related issues only... ask about your iPod problems in the Knowhow Trading Post.
                    )
                    [FORUM] => Array (
                            [NAME] => Hosting Q&A
                            [DESC] => Primarily for discussions about Xepher.net policies, rules, and services.
                    )
            )
            [CAT] => Array (
                    [NAME] => Content
                    [FORUM] => Array (
                            [NAME] => Art
                            [DESC] => General discussion for art and other "visual" media. Mainly for content hosted here, but open for anyone who wants to talk about the visual arts.
                    )
                    [FORUM] => Array (
                            [NAME] => Writing
                            [DESC] => A forum for the discussion of writing and related topics. Open to both members and non-members as a place to discuss stories, essays, and the technical aspects of writing.
                    )
                    [FORUM] => Array (
                            [NAME] => Web Design
                            [DESC] => For the discussion of web design and formatting problems including scripting, database use, etc. A place for general "How do I...?" type of questions not specifically related to Xepher.net services.
                    )
                    [FORUM] => Array (
                            [NAME] => Misc. Content
                            [DESC] => For content that doesn't fall under the other categories. Open to everyone.
                    )
            )
    )
So now that we have the data within the template object's structure we can load the template and parse it. Problem is, this is the part I'm having trouble on, because the array structure above is not technically possible in PHP (you can't have the same key names in an array for different keys). Of course you could write a template system that would have blocks and sub blocks, effectively being 2 layers deep... the problem is, when you run into a situation where you need to go deeper than 2 levels, it instantly becomes ineffective. Later tonight I'm going to convert a few things to include a numerical key base with the first element being the name of the block. I'm thinking that using a recursive algorithm is the only way I'd be able to pull off a dynamic system that can go multiple layers deep (due to restrictions that I can't actually write infinite lines of code doing things like array[0], array[0][1], array[0][1][12], array[0][1][12][2], ... this would make coding the system an extremely long process that would be a pain to change later on).

So at this point I'm open to suggestions as to how to encapluate multiple arrays within one another to create a structure. (another option would be to create a recursive class that would contain objects of itself, kinda like a linked list if possible in PHP).
(\_/)    ~Relakuyae D'Selemae
(o.O)    
(")_(")  [Libre Office] [Chrome]

Xepher

Not to rain on your parade or anything... I mean, it looks like you've put a lot of effort into this and actually have something decent going, but...

http://smarty.php.net/

Databits

I understand that... but there is absolutely no learning involved in using prewritten code to do the work for me.

See... once I finish my website, I'd like to be able to use it as resume material. When an employer says, "so what template system, forum, file manager, user authentication system did you use?" and I say it's all custom written, I'm sure it would be much more impressive than if I simply said, "oh no, I just used smarty". :)
(\_/)    ~Relakuyae D'Selemae
(o.O)    
(")_(")  [Libre Office] [Chrome]

Databits

By the way, just for any others who wish to know about this. The whole template system that I was working on is pretty much discontinued. Anything you could accomplish with that above can (with a bit of effort) be done in smarty, using either a custom XML setup with DOM (a very much better solution in my opinion), or (probably the best method) by using XSLT.

This is simply in response for future people who may be looking into the idea/code behind that above (again). :P
(\_/)    ~Relakuyae D'Selemae
(o.O)    
(")_(")  [Libre Office] [Chrome]

cha0s

Nice. =P I know what you mean about wanting to do it by hand, but in reality most employers today I think would rather that you can pick up any system and be 100% productive with it. =)

Nevertheless, I love to reinvent the wheel. I recently wrote a TCP berkeley socket library for FreeBASIC, in FreeBASIC. (Works on windows and linux) (yes, I am obsessed with that language.)
xepher puts the SEXY in DYSLEXIA

My Music!
FreeBASIC rox hard

Databits

I don't care for it, it's a version of basic, which I gave up long ago. Whether interpreted or supposedly compiled, there is no way it can match the flexability, speed, and preformance that something written right in C++ can. Although it'd be nice if the socket lib for C/C++ was a little more friendly. The fact that it's pretty much the same on Windows/Linux is nice and all, but the suddle differences are a tad annoying. Things like the error codes, a few select functions, and what you need to include to actually make it work. Then you get into multi-threading, something that's not technically related to network programming, but that you should know none-the-less if you're planning on any sort of large scale server.
(\_/)    ~Relakuyae D'Selemae
(o.O)    
(")_(")  [Libre Office] [Chrome]

dragyn

*tries to resist correcting Databits*
*fails*

Subtle, not suddle.

Sorry.

fesworks

Quote from: dragyn*tries to resist correcting Databits*
*fails*

Subtle, not suddle.

Sorry.
*pokes dragyn*

warshcloth :P

www.PSIwebcomic.com
www.TheShifterArchive.com
www.ArdraComic.com
www.WebcomicBeacon.com

Databits

Meh, spelling is a trivial thing on the internet. I usually catch myself most the time, but I still end up spelling things wrong. One of the mistakes I make the most is spelling "the" as "teh", something that's beginning to really get on my nerves actually.
(\_/)    ~Relakuyae D'Selemae
(o.O)    
(")_(")  [Libre Office] [Chrome]

dragyn

Yeah, the "Teh" thing has actually gotten so common, some places actually have spelling-checkers that change "teh" to "the" for you.

Also, "Warshcloth" isn't even a word, and that one only really annoys me in spoken english.  Sorry to disappoint you, Fes.  :rolleyes:

fesworks

Misspelling and mispronuciations are quite similar ;) Thought I'd try to annoy you too :P

In any case, I hate it when people pronounce "WH" (which is a silent "h") like "what" (pronounced "Wut") is "ho-wut" to some people.... same thing when people say "Warshcloth" I'm like... "THERE IS NO "R" IN WASH-CLOTH!"

I'm sure I get more annoyed than you ;)

www.PSIwebcomic.com
www.TheShifterArchive.com
www.ArdraComic.com
www.WebcomicBeacon.com

dragyn

I don't know about that...I can be pretty irritable, especially when the word "especially" is concerned.  There is no "X" anywhere in "especially".

Okay, that's all for now.

fesworks

I used to say 'Egzit" for "Exit" until my *then* girlfriend nagged me about it ;) She's an English Major. ;)

www.PSIwebcomic.com
www.TheShifterArchive.com
www.ArdraComic.com
www.WebcomicBeacon.com

Databits

Hahahahahahaha

You are all obsessed with English spelling and grammar like I am with correcting stupid, wrong, or blatantly broken programming code. :lol:
(\_/)    ~Relakuyae D'Selemae
(o.O)    
(")_(")  [Libre Office] [Chrome]

dragyn

And that is why we talk about english while you wirte helpful guides for those of us less literate in computer codes.