anarres: (Default)
[personal profile] anarres
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.

Date: 2010-06-16 09:31 pm (UTC)
From: [personal profile] dunceheart
I couldn't get your test page to open in IE.8 : (

Profile

anarres: (Default)
anarres

July 2012

S M T W T F S
1234567
891011121314
151617 18192021
222324 25262728
293031    

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 14th, 2025 08:51 pm
Powered by Dreamwidth Studios