pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:

Get Down, Ye Monks!

This might be a stupid question, so any thrown fruit is accepted as due. I've poked around the docs and supersearch, but I don't really know how to phrase the question in a way a search engine can parse...

I'm considering using Template Toollkit for a project. TT seems to require that the templates etc.. come from external files. I just want to process strings. Am I missing something obvious, or is this actually a pretty complex request for TT?

Here's the background: I'm a sysadmin and I've got a zillion little helper scripts that make accounts of various kinds, change quotas, delete things etc... I've finally decided to factor out the common tasks into a module. One of the things I keep writing over and over again is an email to the user in question. These emails are usually %80 identical. I'd like to use TT to make a readily extensible email template. I'd rather keep the template data in the module, rather than spreading out over multiple files because I really don't want these things to get out of sync and I think that'd be easier overall.

UPDATE:

I just stood up, fed the cats, and remembered the real reason I don't want to use external files: when I use this module inside a script, I'd like to be able to manipulate the email from within those scripts without another text file to worry about. My brain is not having a good day. Be merciful...

Thanks!
--Pileofrogs

Replies are listed 'Best First'.
Re: TT & templates from data?
by Fletch (Bishop) on Dec 12, 2008 at 17:52 UTC

    TT's process method will happily work with data from a scalar, you just have to pass a reference to the template text (so that it knows it's not a file name).

    use Template; my $tt = Template->new( ); my $data = { foo => 'bar' }; $tt->process( \qq{[% foo %]\n[% FOR i IN ['A'..'Z'] %][% i %]\n[% END +%]}, $data, \*STDOUT );

    I do this all the time putting the template source after the __END__ marker and then slurping it into a scalar from DATA. Or you could get fancy and use Inline::Files.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: TT & templates from data?
by gwadej (Chaplain) on Dec 12, 2008 at 17:56 UTC

    The POD for Template in Template::Toolkit shows that you can use a reference to a string or a filehandle (DATA is your friend).

    G. Wade

      Ack! Throw fruit now! Sorry everyone. I've been having trouble with the old brain today. :-(

Re: TT & templates from data?
by jeffa (Bishop) on Dec 12, 2008 at 17:32 UTC