Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Custom Module Work

by Anonymous Monk
on Jun 06, 2005 at 18:23 UTC ( [id://464015]=perlquestion: print w/replies, xml ) Need Help??

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

This is a bit of a stretch. But here it goes. I'll list my code first

index.cgi
#!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use vars qw($form); ##################################################### #Import our custom modules used in the main site # ##################################################### BEGIN {push (@INC,"modules/site"); push (@INC,"modules/custom"); } use databaseFunctions; use moduleSecurity; use forms; use template; ######################## #Connect to Database # ######################## our $db = new databaseFunctions; #declare a new database object $db->connect(); #Connect to the database ######################## #Do Form Stuff # ######################## our $form = new CGI; #We need to find out what URL the user is calling and see if its in ou +r #Portal List my $queryString = "SELECT portal from portalUrls where url like '\%$EN +V{'HTTP_HOST'}'"; my $dbSelect = $db->query($queryString); print "Content-type: text/plain\n\n"; #This will be removed, and other + header stuff will be added to each module itself if($dbSelect->{0}{portal} ==0) { print "No Document Found"; } else { #The Portal was found, now to see if its active $queryString = "SELECT active,defaultModule from portalSettings wh +ere id =" . $dbSelect->{0}{portal}; $dbSelect = $db->query($queryString); if($dbSelect->{0}{active} eq "n") { #This is where we would normally print a page saying the porta +l is temporarily unavailable #But for now, this will do print "At this time the site you have requested is unavailable +. Please check back later"; } else { #The Portal is Active. That means defaultModule is available #We need to check if this module is allowed to be used without + authentication my $secCheck = new moduleSecurity(); my $secReturn = $secCheck->checkModule($dbSelect->{0}{defaultM +odule}); my $templateFile = "site/main"; my $tpl = new template; my $returnedtpl = $tpl->parse($templateFile,"print"); if($secReturn eq "y") { print "Security Required"; } else { #my $module = $dbSelect->{0}{defaultModule}; require "$dbSelect->{0}{defaultModule}.pm"; my $returned = $dbSelect->{0}{defaultModule}->mainModule; print $returned; } } }
In the Above code the section
my $templateFile = "site/main"; my $tpl = new template; my $returnedtpl = $tpl->parse($templateFile,"print");
Calls my custom template module as loaded above. This part works fine The value $templateFile = "site/main" is a directory path The contents of "site/main.tpl" is
Some **** <b><%include variable="var1"%></b> <br> <%include module="mod2"%> Some More **** <br> <%include template="site/inctemp"%> <br> Some Random **** <br> <%include menu="menu1"%> <br>
The line
<%include template="site/inctemp"%>
Tells the template module it needs to load yet another template into this location. The template is "site/inctemp.tpl" Then inside "site/inctemp.tpl" it calls "site/inc2.tpl" (these are just development files and have no real world baring) Sounds simple. The template module itself contains this code
package template; use DBI; use strict; use warnings; use variables; BEGIN{ push(@INC,"modules/site"); push(@INC,"modules/custom"); } sub new { my ($class) = @_; my $self = {_template => undef, _method => undef, _filehandle= +>undef}; bless $self,$class; return $self; } sub parse { #use String::Random; #my $ran = new String::Random; my $returnTPL= ""; my ($self,$template,$method) = @_; $self->{_template} = $template if defined($template); $self->{_method} = $method if defined($method); #my $filehandle = $ran->randregex('\w\w\w\'); $self->{_filehandle} = "a121"; if(!(-e "templates/$self->{_template}.tpl")) { #template does not exists; print "Error... No such template exists"; } else { #template does exist open(tpl,"templates/$self->{_template}.tpl"); my $x=0; while(<tpl>) { if($_ =~ /\<\%include/) { #We found a template request, now we need to find +what it is if($_ =~ /\<\%include\s?(\w.*)\=[\"\']([a-zA-Z0-9\ +/].*)[\"\']\%\>/) { my $type = $1; my $val = $2; if($type =~ /variable/i) { my $vars = variables->get(); $_ =~ s/<\%include\s?(\w.*)\=[\"\']([a-zA- +Z0-9].*)[\"\']\%\>/$vars->{$val}/; } if($type =~ /template/i) { #create a new template instance so this te +mplate #doesn't get deleted my $innertpl = new template; my $_innertpl = $innertpl->parse($val,"ret +urn"); print $_innertpl; #$_ =~ s/<\%include\s?(\w.*)\=[\"\']([a-zA +-Z0-9\/].*)[\"\']\%\>/$_innertpl/; } if($type =~ /menu/i) { #query the database for the menu in questi +on #Butcher, and fix my $menu = "The Menu Items"; $_ =~ s/<\%include\s?(\w.*)\=[\"\']([a-zA- +Z0-9].*)[\"\']\%\>/$menu/; } if($type =~ /module/i) { #Activate the modules include function require "$val.pm"; my $returnedData = $val->includeModule(); $_ =~ s/<\%include\s?(\w.*)\=[\"\']([a-zA- +Z0-9].*)[\"\']\%\>/$returnedData/; } } } if($self->{_method} eq "print") { print $_; } if($self->{_method} eq "return") { $returnTPL = join("",$returnTPL,$_); #print $returnTPL; } } close(tpl); } if($self->{_method} eq "return") { return $returnTPL; } } 1;
The Problem...
For whatever reason After it goes into the templates within the templates it stops at the
<%include template>
command and never continues and its beyond me as to why. The real world script is running at http://fidelitysoft.net/cms/ The directories are (http://fidelitysoft.net/cms)
  • /templates/site/
  • /modules/site/
  • /modules/custom/
All files in those directories are readable from the web so you can see the code thats being done The Module that is malfunctioning is template.pm The index script is available at http://fidelitysoft.net/cms/index.txt Can someone tell me why my scripts are not returning after it reads into another template? Your assistance is greatly, hugely appriciated.

Janitored by holli - added readmore tags

Replies are listed 'Best First'.
Re: Custom Module Work
by Hero Zzyzzx (Curate) on Jun 06, 2005 at 18:52 UTC

    It has to be said- what's wrong with HTML::Template or any other of the other excellent templatting modules on cpan.org?. The problem of how to templatize HTML (and all the annoying underpinnings that go along with it) has been solved many times over and made freely available for your use.

    -Any sufficiently advanced technology is
    indistinguishable from doubletalk.

    My Biz

      I've looked at HTML::Template but it seems too complex for my needs. It also doesnt seem to allow me to dynamically let me load perl modules and print the returned data into the template I need to code something powerful, but insanely simple. The program is to allow non tech savvy people write their web pages simpler

        Save yourself some time and use a module that's been *hammered* on thousands of production servers. What do your end users care what's under the hood? If you don't like the tag styles that HTML::Template provides, write an input filter.

        What happens when you decide you need caching? Or loops? Or conditionals? You *will* need them eventually. HTML::Template (and every other templatting system) does this stuff already.

        -Any sufficiently advanced technology is
        indistinguishable from doubletalk.

        My Biz

Re: Custom Module Work
by djohnston (Monk) on Jun 06, 2005 at 21:53 UTC
    Try adding local *tpl; early within the parse method of template.pm. This might get you to second base, but you're still better off with a templating module (from CPAN). Although many boast about how easy it is to write a template parser, it isn't - particularly if you want one that is truely easy to use, extensible, flexible, and so on. I should know, I've (sadly) been reinventing the wheel for many years.

    Update:
    Added the bit in parens.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-16 17:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found