First, I'll begin with a basic HTML file.
This is some sample text
Now that we've established what a basic HTML file looks like, we can go into making it into a template for PHP. Here's what we need to do:
Just like about we have our HTML file; however, whereever we want to have dynamic data, we put a template tag like this:
Save the above file as "sample.tpl".
Once the template class is loaded, you can call the tag function for that tag like this:
// You need to download the template cla
// ss for this to work.
$template = new template();
$template->tag("MyTag","This is some sample text.");
Save the above as "sample.php".
When you run the above file, you should get the following HTML output:
This is some sample text<
Now, you may say, "That isn't that great. Why would I need a class to do that?" Ok, ok. I agree too, but let me show you how this class will make money for you.
Let's just say that you wanted to create a form from some data that was pulled from a database. Here's how you would create that form using this class:
What does this do? Weeelll...When the tag function is called, it checks the tag for the "as" attribute. This attribute tells it how to render the tag. However, you can also override it by using the override attribute of the tag function. Here's the syntax:
tag([tag name],[value],[override tag name]);
If the override attribute is not set, it'll use the default.
So, that's it. There are more tricks that can be done with this class, but I'll save them for another article.