Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Building a Site Engine with PHP, Part 4
  2. You’ve been Tagged
  3. Give it a Little Class:
  4. The Final Words
By: James Murray
Rating: starstarstarstarstar / 24
July 12, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.

if(!strpos(@$load,'-tag-')) $mask='<!-tag->';
$masks=explode('-tag-',$mask);

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.

$loads=array_values($load);
return str_replace($newtags,$loads,$string);
}
}

That’s about it for the replace() function. Now let's move on to how we plan on getting the XML elements from our template to the replace() function.



 
 
>>> More PHP Articles          >>> More By James Murray
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: