HomePHP Page 2 - Building a Site Engine with PHP, Part 4
You’ve been Tagged - PHP
In the last three articles we went over a basic idea of how a site engine works, how to build the plug-in, module, and block systems, the basic directory structure, the database structure, and the authentication methods. In this, the fourth article, we’ll be going over the template system, and the basic page loading methods. So, let’s go ahead and get started.
The tagging system, as I like to call it, is one of the biggest parts of the entire engine, not in size, rather, in functionality. Through out the entire engine we have things being sent to the replace() function, which is the function that replaces all the tags with the correct data, before it’s sent to the browser. So with no further interruptions, let's look at the replace function.
First we define the function and all the parameters it will accept. $load is the array of tags to replace. The array should be set up like this array(‘tagname’=>”data to place where the tag is”); $string is the variable that holds the XML string that we’ll later get from our template using simpleXML; and $mask is an optional variable that you can use in case you want to use a different tag style, such as <*tag>.
function replace($load,$string,$mask='<!-tag->'){
Then we make sure that $load is an array so we don’t get any nasty unexpected errors. If it’s not an array, the function will just return the XML string without processing it:
if(!is_array($load)){ return $string; }else{
If $load is an array, first make sure that there isn’t a tag in the $load named “-tag-“. Then explode the $mask tag by “-tag-“, so that we have an array that has the opening and closing brackets for each tag, “<!” and “>”. Now you might say “why all this stuff about the tags being explode to simply get the ‘<!’ and ’>’.” The reason behind that is like I said before, so you can change the tag style to something else like <*-tag-> if you want to or need too.
Just so you know the part of the previous lines of code “!strpos(@$load,'-tag-')” will produce a warning through PHP because we’re reading an array as if it were a string, however it still works as expected.
Now we’re get to use my favorite PHP functions, array_keys(). array_keys() returns the keys of the input array as the values of the output array. We do this because we need to get the tag name so that we can then build our tag in the function to match it to the tag in the $string to replace it with our data.
Anyway, now we’ll use array_keys() to get all the names of the tags from the $load array we load that into $tags . Then we run the $tags array through a loop to get the value of each of the array keys. Now we build our new list of the actual tags to replace (this is a little easier than searching the XML document to find all the replaceable tags).
$tags=array_keys($load); foreach($tags as $tag){ $newtags[]=$masks[0].$tag.$masks[1]; }
Now we use another built in function that’s just like array_keys() but instead of operating on the keys it operates on the values. It’s surprisingly named array_values(). Then we put it all together and run it through srt_replace() which will loop through the arrays that we pass to it. Str_replace() will return a string with all occurrences of $newtags in $string replaced with values from $load respectively.