TUTORIAL: Fully Functional Simple Template Engine (Usage)
- Posts: 8417
- Loc: USA
This tutorial is made to finish off the Part 1 and Part 2 of my Fully Functional Template Engine series. If there would be any fixes/modifications or whatever else to this class as time goes by, I will post them as a reply to this tutorial and not clutter up the PHP Tutorial sections with one class
This tutorial will not only provide you the usage of this template engine along, but it would also provide some templating tips that you could use to make your life easier.
If you find/have found a fault with my template engine class, feel free to post it here as a reply and I will try to fix it to the best of my abilities.
Before I start telling you about the usage of the template engine, I would like to inform you of some things that I like to do when I create my site that uses any type of template engine. (Right now, I use this class, and would probably never change... since I made this class and it works perfectly fine).
I usually create a global file that would be included into every PHP file in the index directory. Not the classes, but the pages that users would go to, and the pages that would parse any template. I mention something about this in my Basic Templating Mechanism for PHP tutorial. I hope you would find that useful.
In that tutorial, I call the 'global file' as 'Configuration file', but in this case, it has a slightly different purpose. In this case, the global file includes all the other global files, sets global variables and initiates global classes. Quite possibly, that is not the best way to do this and somebody could enlighten me with a reply, but I found this to be quite useful.
Why this is useful? Well, let's say that you have created a class that would be used by a huge majority of your PHP files... and let's add to this scenario that you have a global file included into all of the PHP files that would be viewed by users. What do you do? Well, you include that file into the global file (One line of code into one file) and the file is in turn, included into all of the other files. This saves you time... meaning you don't have to edit all of those files and add that one particular class.
In my usage example given at the beginning of Part 1 and at the end of Part 2 has two global files actually... One at the very beginning and one around the middle. (globals.php and global_vars.php). The reason for those is that globals.php is the global file that includes files, instantiates classes, does functions that should be done every 1+ minutes and whatever... my global_vars.php file sets the variables into the template engine (using $vari ... remember what it is?). This way, certain variables are always allowed in the HTML templates. If you don't want to do that, then you can put in the global_vars.php and include the stuff into the template class itself... that could be easier.
That is why, in the usage example in Part 1 and Part 2, the $tpl->vari is always merged with itself... because global_vars.php already sets it. In theory you should be okay by setting the global variables in the globals.php at the very top, but I like putting them right after I put in $tpl->start() although that shouldn't matter... I just haven't tested it yet.
NOTE: Just to note, the code in the example usage concerning about SQL and the variable $db is part of another, much larger code that has nothing to do with template engine whatsoever. Although that is obvious, I'm just making sure.
The usage of this template engine is pretty simple. The directory structure is basically whatever you like, just as long as you include the class into the PHP file that uses the class to parse an HTML file as a template.
My directory structure as it relates to the template engine is:
- ... and so on ...
In your PHP file where you are going to use the PHP file, include the template class if you aren't using the global file thing, and initiate the class. You may be able to initiate the class in the class file itself, so when you include the file into the PHP file, it is already initiated... something like this:
// the class
$tpl = new tpl();
// the class
$tpl = new tpl();
- // the class
- $tpl = new tpl();
I failed to say this in the previous two tutorials of the template engine, but it fits here perfectly since it is a usage type of deal. The different templates are the names of the directory holding the template HTML files. (Such as the bold folders in my directory example above).
They could be whatever you want, as long as you indicate which template you want to use. If you leave the template indicate variable blank, the template engine wouldn't work. For those of you who are wondering what the template indication variable is...
$tpl->template = 'template1';
That it is... the template indication variable. All it does, is tell which folder in the 'templates' folder to open to find the template HTML files.
Of course, for each page that you will have you would most likely would need a different HTML template page... that is why I put in a template file indication variable. Which is...
$tpl->page = 'index'
Remember... don't put any extension to the filename. What I mean by that is don't put '.html', '.php' or anything else. I probably should allow that and make it so the script assumes that it is '.html' if you don't put it in there, but I hard-coded the extension into the class, so all you need is the name without the extension.
Another use for this variable ($tpl->page) is that it sets the ID for the cache. Since most of your PHP files would require a different HTML template page, I allowed the cache to save the cache as the $tpl->page being the index (What this means is that if your PHP files asks for 'index' ($tpl-> = 'index') then the cache would be saved as index.cache). I really hope that that is clear enough to understand.
So if there is an HTML template used more than once than... it's probably safe enough for you to cache it since the variables in the template file aren't cached, and still could be changed at any time, but I just don't cache those files... since all of those files (at least for me) or information pages that are dynamically created (Such as 'You are not logged in' or 'Your login credentials were incorrect'). Something along those lines.
Both of those variables ($tpl->template and $tpl->page) should be set BEFORE you call on the $tpl->start() function. That function uses those two variables to retrieve the source from the template file.
After that function is called, I like to include the global_vars.php file, do my PHP processing after it... set the $vari array equal to more values (merged to the array already set in global_vars.php) and then generate the template with the function $tpl->gentemp($var) where the underlined variable ($var) is true if the template is to be cached, or false if it isn't to be cached. Pretty simple and nothing to it
If you want to view all of the templates available to you, just call on the list_styles() function. What that function does is, it iterates through the templates folder and echoes the names of all folders (Not files such as index.php, but folders) in that folder. Those folders are automatically assumed to be a template style.
Well, this tutorial basically concludes the whole series of the Fully Functional Simple Template Engine tutorials. If you feel like I have left something out in any of these tutorials, feel free to reply here and tell me about it... then I will feel free to reply with (hopefully) an answer
Thanks for reading.
Page 1 of 1
- Total Posts in this topic: 1 post
- Moderator: Tutorial Writers
- Users browsing this forum: No registered users and 3 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum