Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Template::Plugin::DBI question

by avo (Pilgrim)
on Sep 04, 2008 at 08:53 UTC ( [id://708965]=perlquestion: print w/replies, xml ) Need Help??

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

Good day monks,
I am using Template::Plugin::DBI a lot lately. I however am including the following in all of my templates on top:
[% USE DBI("DBI:mysql:$database;host=$dbhost", "$dbuser", "$dbpassword") %]
Is there a way to go around that. I am using also Template::Plugin::CGI which I don't need to use in my template body because I simply have a line like this (in my program):
$vars{mycgi} = new Template::Plugin::CGI;
I just have no idea how can I do that with DBI plugin. Any suggestions ?

Replies are listed 'Best First'.
Re: Template::Plugin::DBI question
by shmem (Chancellor) on Sep 04, 2008 at 09:33 UTC
    Any suggestions ?

    Yes. Don't! The need alone to do that in all of your templates should tell you that something is utterly wrong with that approach. You repeat yourself, and you are writing nasty PHP-level code from years ago.

    Templates are meant for separating task, i.e. data gathering and data rendering. Templates are for rendering data, so database connections don't belong in a template. See e.g. The Model View Controller pattern in web applications for a good start into Model View Controller (MVC).

    update: Template::Plugin::DBI is a clever module which eases doing the wrong thing, imho. I'd never use it.

Re: Template::Plugin::DBI question
by Your Mother (Archbishop) on Sep 04, 2008 at 20:11 UTC

    You can do it the same was way, almost, as the CGI and you should probably modify your CGI call. Something like this–

    my %vars = ( cgi => CGI->new(), dbh => DBI->connect([connection params]), );

    You can just follow the regular docs for those modules then. It's sometimes terribly convenient to code DB calls and such inside templates (hence PHP's rapid rise) but it's usually a bad long term design choice.

Re: Template::Plugin::DBI question
by duelafn (Parson) on Sep 04, 2008 at 13:56 UTC

    Try putting your USE DBI directive in a separate template then use Template's PRE_PROCESS option.

    Good Day,
        Dean

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-24 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found