Archive for April, 2009

Tpl – A tiny PHP template engine

While developing CodeScape Framework I came up with a simple template engine that instead of implementing it’s own language just provided some structure-related hooks, and I’ve now got around to separating out and releasing Tpl, a simple, self-contained, hierarchical template engine for PHP.

Tpl’s role is centred around template structure, not content—it’s not a huge library of helper functions for creating links, displaying images, etc.  Like any template engine, the aim is to separate presentation logic from application logic, but it doesn’t attempt this by creating a template language.  PHP is designed to be interleaved with “static” output, which means it’s already a template language.  Trying to replace it for the purpose of templating rarely adds anything useful, incurs hefty performance overheads (which you then have to mitigate with caching) and usually is more restrictive than PHP.  To me, that doesn’t seem like a sensible application of effort.  Instead Tpl provides its functionality in the form of hooks—functions which are called from within the template to specify the structure. Instead of PHP being replaced, it is extended.

The main motivating factor for creating Tpl is that all PHP template engines seem to be centred around inclusion rather than inheritance, something I only noticed after spending time working with Django.  For example, a typical PHP template might look like this:

<!-- header.php -->
<html>
    <head><title><?=$title?></title></head>
    <body>
        <div id="header">Website Name</div>
<!-- footer.php -->
        <div id="footer">Some footer text</div>
    </body>
</html>
<!-- content.php -->
<? include('header.php'); ?>
 
<div id="content">
    Some content
</div>
 
<? include('footer.php'); ?>

The main problem with this is that it bears no real relation to the logical structure of the page, just the order in which stuff should appear in the HTML. If you modify something in the header, you need to make sure you’ve updated the footer too. The equivalent Django template would look like this:

{# base.html #}
<html>
    <head><title>{{ title }}</title></head>
    <body>
        <div id="header">Website Name</div>
 
        {% block content %}{% endblock %}
 
        <div id="footer">Some footer text</div>
    </body>
</html>
{# content.html #}
{% extends "base.html" %}
 
{% block content %}
<div id="content">
    Some content
</div>
{% endblock %}

The point to note is that the structure is coherent and in one place, and the parts that change are defined as logical blocks which can be replaced. This is the method I’ve adopted in Tpl—the equivalent template would look like this:

<!-- base.php -->
<html>
    <head><title><?=$C['title']?></title></head>
    <body>
        <div id="header">Website Name</div>
 
        <? Tpl::block('content'); ?><? Tpl::endblock(); ?>
 
        <div id="footer">Some footer text</div>
    </body>
</html>
<!-- content.php -->
<? Tpl::inherit('base.php'); ?>
 
<? Tpl::block('content'); ?>
<div id="content">
    Some content
</div>
<? Tpl::endblock(); ?>

More information about Tpl and how to download it can be found at the Tpl project page.

Enable Bitmap Fonts on Ubuntu Jaunty

I like to use tiny bitmap fonts like MonteCarlo for programming, but by default Ubuntu has bitmap font support turned off.  From (at least) Gutsy through to Intrepid, this method worked for enabling bitmap font support, but after installing the Jaunty beta I found this no longer works.

Luckily, after a brief look in /etc/fonts, I found that font configuration now follows the nice pattern of a conf.avail directory containing all the available configuration parts, and conf.d containing symlinks to these parts.  This makes enabling bitmap fonts even simpler now:

# "Un-disable" bitmap fonts
sudo rm /etc/fonts/conf.d/70-no-bitmaps.conf
# Clear the font cache
sudo fc-cache -f -v

Now you should be able to drop bitmap (i.e. PCF) fonts into ~/.fonts as you would with TTF fonts and be able to use them with no extra hassle.

Update: Andreas asked in the comments if there was a way to do this that doesn’t require root access. In fact there is! If you copy /etc/fonts/conf.avail/70-force-bitmaps.conf to ~/.fonts.conf then it has the same effect. (If you already have a ~/.fonts.conf then you’ll want to merge them together – copy everything inside <fontconfig>...</fontconfig> from 70-force-bitmaps.conf and put it just before </fontconfig> in ~/.fonts.conf)

VDB 0.1.0 (alpha) Released

Recently I’ve been working on a program that will let me do a full-text search of my 1500-file digital video library.  The result is something I’ve unimaginatively called “VDB”, short for “Video Database”.

VDB InterfaceThis release doesn’t do much: you can add your library of video files, and it will let you do a full-text search on the filename.  You can double-click an item in the list to launch the video with ‘gnome-open’ (which should honour user settings for preferred applications).

Feel free to download it, mess around with it, and send me any feedback you have.  I have plenty of future plans for it, including “watch folders”, tagging, metadata, fetching information from IMDB, and being able to search on all of this information.  (Think queries like “actor:’johnny depp’ tag:crime”.)

Visit the VDB project page.