Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Template Toolkit and Dynamic Database Lookups

by arunhorne (Pilgrim)
on Jan 06, 2006 at 15:00 UTC ( [id://521490]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I want to combine the Template Toolkit with dynamic database lookups

Usual use of the TT is;

  • 1) write a template
  • 2) populate a map of keys to variables
  • 3) process template passing map into it

This assumes that I know all the variables that need to be defined in the map prior to calling process().

What I would like to do is process a template and when a variable is encountered, lookup it up in a database to find it's value. Now, the first impulse is to just load the map from the database, but I do not know ahead of time what variables will be needed (and the database is huge). So I would like to dynamically look up variables during the process function.

Does anyone know how to do this or something similar with Template Toolkit? Are there better ways to do it?

Thanks in advance

____________
Arun
  • Comment on Template Toolkit and Dynamic Database Lookups

Replies are listed 'Best First'.
Re: Template Toolkit and Dynamic Database Lookups
by rinceWind (Monsignor) on Jan 06, 2006 at 15:07 UTC

    Check out Template::Plugin::DBI that comes with the Template Toolkit distribution.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      I was just looking at that yesterday. There doesn't seem to be a way of doing the following without querying the database twice:

      if no records match (content) else foreach record (content) end end

      It seems like something people would commonly need.

      I was thinking of making an alternate version of Template::Plugin::DBI::Iterator where the content of get_first has been moved into new and where get_first is a dummy. One could then do the following:

      [% RAWPERL %] local $Template::Plugin::DBI::ITERATOR = 'Template::Plugin::DBI::IteratorZ'; [% END %] ... [% SET query = DBI.query( 'SELECT rows FROM table' ) %] [% IF query.last %] content [% ELSE %] [% FOREACH item = query %] content [% END %] [% END %] ... [%# Still works the old way %] [% FOREACH item = DBI.query( 'SELECT rows FROM table' ) %] content [% END %]

      (Untested)

      Is there a better way? Or any other way?

      Update: Added "without querying the database twice" and the "still works the old way" bit.

        You could always run a SELECT COUNT(*) FROM table... first, to find out whether you have any content.

        --

        Oh Lord, won’t you burn me a Knoppix CD ?
        My friends all rate Windows, I must disagree.
        Your powers of persuasion will set them all free,
        So oh Lord, won’t you burn me a Knoppix CD ?
        (Missquoting Janis Joplin)

Re: Template Toolkit and Dynamic Database Lookups
by perrin (Chancellor) on Jan 06, 2006 at 18:43 UTC
    Class::DBI can basically do what you're asking for. See this article.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://521490]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-25 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found