Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jjohn (22)

jjohn
  (email not shown publicly)
http://taskboy.com/
AOL IM: taskboy3000 (Add Buddy, Send Message)

Perl hack/Linux buff/OSS junkie.

Journal of jjohn (22)

Thursday January 23, 2003
10:28 PM

Text::Template

[ #10151 ]

For State Secrets, I'm thinking about breaking out the interface into templates. These can be better manipulated by end users. Since I want SS to run on Winders with as little fuss as possible, I want something that PPM will install without fuss. Template Toolkit, my first choice, is unknown to PPM. Bummer. MJD's Text::Template is not. Here's a script that morphs Text::Template into something TT-ish. Note that does not get TT syntax with this, but it's close enough. I present this hear without intelligent comment so that I can find on the web later.

The processor a command line tool

#!/usr/bin/perl --
# See if I can make Text::Template more TT2 like

use strict;
use warnings;
use Text::Template qw(fill_in_file);

use constant TEMPLATES => './templates';
use constant SOURCE    => './src';
use constant CONSTANTS => './templates/constants';

my $infile = (shift @ARGV || "");
while (! $infile || ! -e SOURCE . "/$infile") {
  print "Which file should I process? \n";
  $infile = <>;
  chomp $infile;
}

get_constants(CONSTANTS);

my $config = {
              TEMPLATE_DIR => TEMPLATES,
              SOURCE_DIR   => SOURCE,
              # template functions
              include      => \&include,
             };

my $processor = Text::Template->new(TYPE => 'FILE',
                                    SOURCE => SOURCE . "/$infile",
                                    DELIMITERS => ['[%', '%]']
                                   );

my $text = $processor->fill_in(HASH    => $config,
                               PACKAGE => "__CONSTANTS",
                              );
print $text, "\n";
#------
# subs
#------
sub get_constants {
  my ($file) = @_;

  return unless -e $file;

  package __CONSTANTS;
  do($file) or die "Can't parse $file: $@";
  package main;

  return;
}

# includes happen in the templates dir
sub include {
  my ($file, %args) = @_;

  return fill_in_file(TEMPLATES . "/$file", HASH => \%args);
}

Templates

header

<html>
<title>{ $title }<title>
<body>

footer

</body>
</html>

Constants roughly like TT's 'config'

$foo = "bar";
# This is a nutty test
%requires = (bar => 1, b =>2);

$sam = [0,3,5];

sub hairy {
  "I like beans!\n";
}

Source File

hello.html

[% include('header', title => 'hello') %]
Hello, [% $foo %]

Hello, [% hairy() %]
[% include('footer') %]

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • There are Template-Toolkit ppms available for 2.08 [sourceforge.net] and 2.08c [sourceforge.net]. Apparently PPM3 isn't yet able to install from .ppd files, so you might need to use PPM2 if you're not already.
  • I couldn't install Template using the default settings of PPM, so I think I'll pursue Text::Template for now. Thanks for the tip.