I think its time to redo the whole walljm site. from scratch. admin, design, architecture, rss, all of it.

I tell you this, because I assume you are interested in the minutia. and because publishing is free. and because i’ll probably not do it otherwise. and because i needed to post something new. ;) 

MovableType Templating for WordPress

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);
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.



Marginal Revolution: Blogging as self-experimentation “Blogging makes us more oriented toward an intellectual bottom line, more interested in the directly empirical, more tolerant of human differences, more analytical in the course of daily life, more interested in people who are interesting, and less patient with Continental philosophy.” 

Nobody knows who Rance is

Least of all me. Rance has a blog though, and its making its rounds in the media. I’m not big fan of memes or fads, but my roomate is currently in DC studying screenwriting, and I thought he might enjoy this supposed look into the lives of a real hollywood actor or actress, whatever the case may be. :)