Text::Template

jjohn on 2003-01-24T03:28:23

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


{ $title }<title>
<body>

</pre>

<p>footer
<pre>
</body>
</html>


</pre>

<p><b>Constants</b> roughly like TT's 'config'
<pre>
$foo = "bar";
# This is a nutty test
%requires = (bar => 1, b =>2);

$sam = [0,3,5];

sub hairy {
  "I like beans!\n";
}
</pre>

<p><b>Source File</B>
<p>hello.html
<pre>
[% include('header', title => 'hello') %]
Hello, [% $foo %]

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

</pre>
</p>


<hr/>



<h2>TT PPM</h2>
<h3><a href="/user/lachoy/">lachoy</a> on 2003-01-25T15:22:55</h3>
There are Template-Toolkit ppms available for <a href="http://openinteract.sourceforge.net/ppmpackages/Template-Toolkit.ppd" title="sourceforge.net">2.08</a sourceforge.net> and <a href="http://openinteract.sourceforge.net/ppmpackages/Template-Toolkit-dev.ppd" title="sourceforge.net">2.08c</a sourceforge.net>.<br/><br/>Apparently PPM3 isn't yet able to install from<nobr> <wbr></nobr>.ppd files, so you might need to use PPM2 if you're not already.



<blockquote>

<h2>Re:TT PPM</h2>
<h3><a href="/user/jjohn/">jjohn</a> on 2003-01-26T04:38:39</h3>
Yikes! That does give me something to think about. mmm. Thanks for the info.





</blockquote>


<h2>PPM2 and 3 give me the finger</h2>
<h3><a href="/user/jjohn/">jjohn</a> on 2003-01-27T03:46:56</h3>
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.





</div> <!-- /span8 -->

</div> <!-- row -->
</div> <!-- /container -->



    <!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->

  </body>
</html>