Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

php style includes

by BigVic (Initiate)
on Jul 02, 2001 at 22:47 UTC ( [id://93275]=perlquestion: print w/replies, xml ) Need Help??

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

it is i again

php style includes look like this:

include("templates/top.html"); include("templates/left.html"); include("templates/main.html"); include("templates/bottom.html");

my perl equivalent for several templates at a time

foreach $plate(keys %PLATES) { $TEMPLATES{$plate} = "template_folder/$plate"; if (open ($handle,"$TEMPLATES{$plate}) )<br> { $TEMPLATES{$plate} = <$handle>;<br> close ($handle);<br> } } print $TEMPLATE{'top'} print $TEMPLATE{'left'} print $TEMPLATE{'main'} print $TEMPLATE{'bottom'}
this is a rough draft of what i'm trying to do, there might be some erros in the syntax but i'm sure you can grasp the concept. now this script is going to be called several times from a site menu with several different template options, (i'm just trying to make it simple for explantion purposes). which means that file opening and closing is going to happen very often, which will slow down the site. is there a better way of doing this? remind you, SSI is not an option, neither is CGI.pm, and no stander SQL style database is available.
the dumb boy seeks wisdom
............................................................................
If only there were evil people somewhere insidiously committing evil deeds and it were necessary only to separate them from the rest of us and destroy them. But the line dividing good and evil cuts through the heart of every human being. And who is willing to destroy a piece of his own heart?
-Alexander Solzhenitsyn, novelist, Nobel laureate (1918- )

Replies are listed 'Best First'.
Re: php style includes
by chromatic (Archbishop) on Jul 03, 2001 at 01:10 UTC
    There's always the Orcish Manouvre. Simply keep around a hash of template names and lazily evaluate them if necessary:
    my %templates; # some code sub include { my $tpl = shift; $templates{$tpl} ||= fetch($tpl); return $tpl; } sub fetch { my $tpl = shift; local (*TPL, $/); open(TPL, "template_folder/$tpl") or return; return <TPL>; }
    It may look very different, and you can get away with tieing the hash to do lazy lookups. The important point is that, if you have a persistent process that may need to read several files, open them only when necessary and store them all other times.

    If your process isn't persistent, then there's not a lot you can do. Open them only when you need to, and pray to the virtual memory subsystem that they stay in cache.

Re: php style includes
by thabenksta (Pilgrim) on Jul 02, 2001 at 22:52 UTC
         How about HTML::Template
      Agreed. If you use HTML::Template, you can use include statements within your HTML documents. Something like this could go in your index.html (or where ever):
      <html> <body> <tmpl_include="templates/top.html"> <tmpl_include="templates/left.html"> <tmpl_include="templates/main.html"> <tmpl_include="templates/bottom.html"> </body> </html> In your Perl code, you would just say: <code> use HTML::Template(); # open the html template my $template = HTML::Template->new(filename => 'index.html'); # Fill in various parameters here ... # Send header here ... # print the template print $template->output;
      The documentation for this module is very good, just read up on it for instructions on what all you can do with it.
      -Eric
      HTML::Template IS NOT AVAILABLE.
        if you have the ability to create a .pl file and directories on the server then you can use really any module.

        I never bother my hosting provider with module installs, instead I have a perl-lib dir in my home directory with around 5 megs of modules. You just need to add the path to your perl files. I use something like this:

        use lib ("/home/myusername/perl-lib");
        @INC stuff
        Use Lib() and require
        perlman:lib:lib

        If you're absolutely intent on not using any modules, try implementing you own handler:

        for (@page) { s/include\("([^"]+)"\)//;print; if(open F $1){while(<F>){print}} }
        this one should work with php style tags (what you showed). it's incredibly insecure and would miss alot, and may not even work, but it demonstrates the idea

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-18 20:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found