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:[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:
categories
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:
[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).