Wednesday, April 24

MovableType Templating for WordPress

6

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.


<?php
require_once(TEMPLATEPATH.'/phptemplate/Template.php');
$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);
echo $te->build_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
  • LinkCategories
  • Links: when Links is used inside the LinkCategories tag, Links will only return those links in that link category.
  • Authors
  • 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.

 

Share.

6 Comments

  1. But you don’t power your blog with WordPress! So the templating structure shouldn’t affect you… *is puzzled*

  2. yes but his brother(me) uses it at least for a little while. And some other blogs he manages for family and friends use it. Thus the desire to fix an unwieldy system. If I didn’t have so many other projects to work on I would have tackled it myself.

  3. Whoa! I almost thought I’d hit Jeremy’s blog instead of yours when all this coding popped up. LOL

  4. irene: I had a lot of free time at work for a couple weeks ago, so I ported the code as an exercise to keep me busy. But the WP templating has always just bugged me. I run a couple blogs that use WP as the blogging engine, http://www.ablogapart.com, and some other ones. :)

  5. Well, I know nothing about WordPress templates, except that I was using Blogsome.com for awhile (powered by WordPress) and the template was hellish to wade through, since I don’t know anything about PHP :P

    Give me MovableType any day… their templates make a lot more sense to me and are easier to mod ;)