anarres: (pic#456045)anarres ([personal profile] anarres) wrote in [site community profile] dw_dev_training,
@ 2010-06-16 03:02 pm UTC
  • Previous Entry
  • Add to Memories
  • Tell someone about this!
  • Next Entry
Entry tags: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.


(Read 4 comments)

Post a comment in response:

From:
Anonymous( )Anonymous This account has disabled anonymous posting.
OpenID( )OpenID You can comment on this post while signed in with an account from many other sites, once you have confirmed your email address. Sign in using OpenID.
User
Account name:
Password:
If you don't have an account you can create one now.
Subject:
HTML doesn't work in the subject.

Message:

 
Notice: This account is set to log the IP addresses of everyone who comments.
Links will be displayed as unclickable URLs to help prevent spam.