Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Web app frameworks - I am totally confused!

by Roger (Parson)
on Oct 06, 2003 at 03:21 UTC ( #296847=note: print w/replies, xml ) Need Help??


in reply to Web app frameworks - I am totally confused!

One technique I often used is a simple HTML template with data tags wrapped inside %% quotes. Simple and effective.
#!/usr/bin/perl -w use strict; use CGI; # Initialize CGI variable my $q = new CGI; # Load HTML my $html; { local $/; $html = <DATA>; } # Format HTML using data callbacks $html =~ s/%%(\w+)%%/&FormatHTML($1)/ge; # Display the HTML print $q->header, $html; exit(0); sub FormatHTML() { my $tag = shift; if ($tag eq "TITLE") { return &FormatTitle(); } elsif ($tag eq "TOC") { return &FormatToc(); } elsif ($tag eq "BODY") { return &FormatBody(); } } sub FormatTitle() { return "HTML Sample"; } sub FormatToc() { return "<b>Table of Contents</b><br>"; } sub FormatBody() { return "<i>Sample text</i>"; } __DATA__ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>%%TITLE%%</title> </head> <body> %%TOC%% %%BODY%% </body> </html>

Replies are listed 'Best First'.
2Re: Web app frameworks - I am totally confused!
by jeffa (Bishop) on Oct 06, 2003 at 04:48 UTC
    I don't see how your example is more simple than the following:
    use strict; use warnings; use CGI qw(header); use HTML::Template; my $tmpl = HTML::Template->new(filehandle => \*DATA); $tmpl->param( title => 'HTML Sample', toc => 'Table of Contents', body => 'Sample text', ); print header, $tmpl->output; __DATA__ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title><tmpl_var title></title> </head> <body> <b><tmpl_var toc></b><br/> <i><tmpl_var body></i> </body> </html>
    Barring looking under the hood of HTML::Template, of course (hairy stuff - but it works, it's tested, it's ready to go, and it handles loops and includes).

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      I can't resist. :)
      use strict; use warnings; use CGI qw(:header); use Template; print header; my $t = Template->new(); $t->process(\*DATA, { title => 'HTML Sample', toc => 'Table of Contents', body => 'Sample text', }) or die $t->error(); __DATA__ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>[% title %]</title> </head> <body> <b>[% toc %]</b><br/> <i>[% body %]</i> </body> </html>
      Of course this example barely shows off anything..

      Makeshifts last the longest.

      I have never used HTML::Template before, seems like a nice and simple way to do templates. Thanks for the tip I will have a look at the module. My solution was a hack I used from time to time without the HTML::Template. The principles applied should be the same.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://296847]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2021-06-17 06:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (83 votes). Check out past polls.

    Notices?