anarres: (Default)
anarres ([personal profile] anarres) wrote in [site community profile] dw_dev_training2010-06-16 03:02 pm
Entry tags:

My epic quest to learn Template Toolkit

I thought this might be useful for anyone who's as baffled by Template Toolkit as I am ;-)

First off, I just wanted to get something, anything, to work, so I would be able to play with it. It turns out there are a few different ways to use Template Toolkit, and Dreamwidth uses it via the Perl Template module. I installed this module on my laptop by doing this:

$ sudo cpan Template

Then following this tutorial: Devshed: getting started with the Perl Template Toolkit, I put the following two files in the same directory:

destruction.tt:

People of [% planet %], your attention please.

This is [% captain %] of the Galactic Hyperspace Planning Council.

As you will no doubt be aware, the plans for development of the outlying regions of the Galaxy require the building of a hyperspatial express route through your star system, and regrettably your planet is one of those scheduled for destruction.

The process will take slightly less than [% time %].
Thank you.



destruction.pl:

#!/usr/bin/perl

use strict;
use warnings;
use Template;

my $tt = Template->new();
my $input = 'destruction.tt';
my $vars = {
planet => 'Earth',
captain => 'Prostetnic Vogon Jeltz',
time => 'two of your earth minutes',
};

$tt->process($input, $vars)
|| die $tt->error();


When you run destruction.pl, it prints out destruction.tt, but replaces '[% planet % ]' with 'Earth', '[% captain %]' with 'Prostetnic Vogon Jeltz', and '[% time %]' with 'two of your earth minutes'. So, basically, you have a document foo.tt that includes [% something %], and you have some other file foo.pl, which uses the Template library to define what [% something % ] is.

That's all well and good, but I wanted to look at how Template Toolkit works in Dreamwidth. At first I was horribly confused because I tend to assume that if there is a page http://www.dreamwidth.org/about.html, then there must be a file on the dreamwidth server ~/dw/htdocs/about.html, but that's not how it works with TT. yvi pointed out to me that the files that generate TT pages actually live in ~/dw/views/. for example ~/dw/views/misc/about.tt looks like this:

[%- sections.title='About Dreamwidth Studios' -%]

Open Source. Open expression. Open operations.

Dreamwidth Studios is an Open Source social networking, content management,
and personal publishing platform. Our mission...(lots more text cut out)


As an experiment I created a new file on my dreamhack, ~/dw/views/misc/test.tt:


[%- sections.title='hello world' -%]
test test test

I eagerly navigated to http://www.anarres.hack.dreamwidth.net/test, but it didn't work, I just got a 404 error. So what does about.tt have, that my newly created test.tt doesn't have? The answer is this file: ~/dw/cgi-bin/DW/Controller/DreamwidthMisc.pm:


#!/usr/bin/perl
# (I cut out the comments to make this shorter)

package DW::Controller::DreamwidthMisc;

use strict;
use warnings;
use DW::Controller;
use DW::Routing;
use DW::Template;

DW::Routing->register_static( '/about', 'misc/about.tt', app => 1 );

1;


The second-last line is telling it that the url http://www.anarres.hack.dreamwidth.net/about should point to the file ~/dw/views/misc/about.tt. So I tried adding to this file the line:


DW::Routing->register_static( '/test', 'misc/test.tt', app => 1 );


I reloaded http://www.anarres.hack.dreamwidth.net/test and this time it worked! So, at the simplest possible level, that is how you would make a Dreamwidth page in Template Toolkit. Of course it can get a lot more complicated than that, but it's nice to be able to start with something simple. I think the next step at this point is to read Routing and Template Toolkit.
cesy: "Cesy" - An old-fashioned quill and ink (Default)

[personal profile] cesy 2010-06-16 02:11 pm (UTC)(link)
Thanks for posting this! It'll be useful when I finally get onto one of my assigned bugs.
pauamma: Cartooney crab wearing hot pink and acid green facemask holding drink with straw (Default)

[personal profile] pauamma 2010-06-16 03:42 pm (UTC)(link)
Thanks for posting this! (And I owe you a bunch of replies on other stuff - I haven't forgotten)
denise: Image: Me, facing away from camera, on top of the Castel Sant'Angelo in Rome (Default)

[staff profile] denise 2010-06-17 12:00 pm (UTC)(link)
This is a really awesome post. As someone who's been completely baffled by TT, thank you!
kareila: (Default)

[personal profile] kareila 2010-06-17 05:24 pm (UTC)(link)
Yes, this is a great intro. Thanks!