HomePHP Page 4 - Building a Site Engine with PHP, Part 1
The Template Got All GUI - PHP
Content management systems are becoming very popular, but what if you took it a step further than that? Thatís exactly what I plan to show you how to do in this series of articles. Iím going to explain how to build a site engine. A site engine is a core code base and database that can run multiple sites that are completely different while all running in unison, using all the same code, yet separate from each other.
Even though I've loaded all my blocks into a multidimensional array with keys that correspond to the block's placement, I still need a way for the engine to know what the coordinates made up of the array keys mean. That's why we need a template system and a loading system. What this system does is it looks at the array of loaded blocks and puts them in place on the template.
The templates are all XML based, to speed up the parse time, and with the new SimpleXML functions of PHP5, it makes it very easy to access the template data. The first thing you'd need to decide how you want the layout to look on the site. I prefer the traditional 3 column layout because it maximizes usable screen space. However it can be set up anyway you'd like, whether it's 3 columns wide, 1 column wide or even 5 columns wide.
When the multidimensional block array is made it looks something like this:
$blocks - all the blocks in column 1
$blocks - all the blocks in column 2
$blocks - all the blocks in column 3
And so on until the final column. As you can see, the array key determines what column of the layout the blocks will show up in. The rows are just the same as the columns, and each of those arrays are sorted in the order that the blocks with be outputted from top to bottom by specifying the row as the key. Consider the following:
$blocks - the top block in the first column
$blocks - the third block in the second column
$blocks - the top block in the third column
Here's a small visual mock up of the layout that contains 3 columns 2 blocks in column 1, 2 blocks in column 2, and 1 block in column 3.
It's pretty simple and logical when you think about it.
The template file also includes the look of the blocks, which also contains tags that will be replaced with the data that the block has such as the title and the block content. The blocks are actually each a separate file that is linked to the database by the file path then called through the plug-ins. The reason for this is because you might have a block that contains no PHP, just some HTML such as a menu block. So we replace the block template content tag with an include of the block file when the block is loaded. The blocks are by far the most confusing part of the entire site engine, but while working on it, you'll get to know and understand how it works a lot better.