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

Hello dear monks,

we are currently planning to invole the Perl Template Toolkit into our Web Development - in the past we just mixed code and layout - a common problem!

Template Toolkit is our choice - especially the ability to compile Template files into much faster Perl Code is working great, and fast.

Now I am wondering, if it is possible to directly compile Templates from our database. Think of this task: The perl script retrieves the Template Layout from our database at script start.

Then the Script should be able to compile the Template to perl-code, and just run from the perl-code! This is - as i see it - only possible if I retrieve the Template files from the file system.

eg. if I use this Template file:

/templates/one.tmpl

it creates:

/templates/one.tmpl.pp (compiled template)

Is it possible to use the compiling mechanism when retrieving data directly from Database? e.g compiling the Template into a (referenced) scalar and just run this scalar?

Many Greetings
chingomaster

Replies are listed 'Best First'.
Re: Perl Template Toolkit / DB Caching
by gam3 (Curate) on Mar 30, 2005 at 12:27 UTC
    use Template::Parser
    use Template::Parser; $parser = Template::Parser->new(\%config); $template = $parser->parse($text) || die $parser->error(), "\n";

    Template compiles the template in the subroutine on line 1512 of Template::Provider.

    -- gam3
    A picture is worth a thousand words, but takes 200K.
Re: Perl Template Toolkit / DB Caching
by lachoy (Parson) on Mar 30, 2005 at 12:57 UTC

    If you write your own Template::Provider you can have INCLUDE/PROCESS look first to the database to load templates. If cached compiling is on TT will then serialize the compiled template to disk, and should look there first before trying to load a raw template from the database.

    Chris
    M-x auto-bs-mode

Re: Perl Template Toolkit / DB Caching
by merlyn (Sage) on Mar 30, 2005 at 13:59 UTC
Re: Perl Template Toolkit / DB Caching
by ryantate (Friar) on Mar 30, 2005 at 19:30 UTC

    I remember seeing this in the docs --

    http://www.template-toolkit.org/docs/plain/FAQ/FAQ.html#Can_I_serve_templates_from_a_database_

    Unfortunately the answer is a reference to "the mailing list" archives but not a link. Oh, it does say to subclass Tempalte::Provider.

    There are multiple mailing lists and they don't seem to be indexed. A google search on site:template-toolkit.org didn't turn up anything useful. Oh well. The other answers in this thread probably suffice, but it would be nice to see this upstreamed into the TT2 docs.