I don’t know about you, but I’ve always disliked the WordPress templating structures. I’ve had some spare time over the last few weeks, so I decided to take some templating code I had worked on previously and port it to php.
phptemplate is a simple, fast and flexible templating system that can be used in any php application. It provides a fast way to build templated code, and offers a way to clearly separate code from content.
PHPTemplate has three main main files:
Parser.php : the parsers job is to parse a string and return a tree. It is a php class and has two functions:
parse_text takes two variables, $textblock is the text to be parsed, $tagID is the prefix to the tags which defaults to BK. So the default tags will look like this, <BKTag /> and <BKContainerTag>this is some block text</BKContainerTag>
parse_attributes gets called internally by the parser and will rarely be needed outside. It take once variable, $atag, and returns a hash with the attribute/value pairs.
TagHandlers.php : tag handlers are functions that get called when a tag is found. Tag handlers mirror the name of the tag, so if there is a tag called <BKFirstName />, then there will be a function called "FirstName()". Most tag handlers return a string which is compiled by Template.php and eventually written out to the page. Tag Handerls are passed two variables:
$token is the tag token object, is a hash, and has four items: $token->type (container|single), $token->tagname , $token->block (if it is a container tag, this is the contents of the container), and $token->attributes (a hash of attribute/value pairs). In the case of a container tag, it is the responsibility of the tag handler to call template.php’s call_handlers function and pass it the $token->block.
$parameters is a variable containing anything you choose to pass through. It is often used by container tags to pass data to its subtags.
Template.php : the job of template is to call the parser, then call the tag handles for the tags it finds. It is a php class and when created, you pass it the text of the template. You then need to call the build_template() function, optionally passing at parameters in that you might need later. build_template returns a string, the result of the parsed template text.
To use phptemplate in wp, simple copy the phptemplate dir into your theme, and for any file you wish to use templating, paste this code into the php file and create a .tmpl file of the same name containing your template code.
$tmpfile = substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], "/")+1);
$tmpfile = substr($tmpfile, 0, strrpos($tmpfile, "."));
$template = file_get_contents(TEMPLATEPATH."/".$tmpfile.".tmpl");
$te = new Template($template);
I have included the default theme with a phptemplate example as a demonstration. Tags I have currently ported from wordpress are listed below:
Posts: replaces theLoop
Links: when Links is used inside the LinkCategories tag, Links will only return those links in that link category.
Categories: may be used in either a container or a single tag context.
I will be updating this post to provide more complete documentation of the wp tags i have ported. You can download the example of phptemplate in wp here.