Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Meditations on templates

by petethered (Pilgrim)
on Apr 19, 2001 at 12:55 UTC ( [id://73774]=perlmeditation: print w/replies, xml ) Need Help??

I use, and write, large numbers of CGI programs designed to do various tasks. Everything from random display of data, to complicated programs that do everything but flush the toilet for you.

One thing I come across alot in other peoples coding is the lack of templating. Programs that have the HTML hardcoded into it, making the programs inflexible and a pain to customize.

Whenever I code, every piece of HTML outside of table internals ( and sometimes even them ) is templated. It makes code easier to read, and the software easier to work with.

If you are one of those people who hardcode their HTML allow me to atleast give you a head start to my perceived path of "good" cgi programming.

The template subroutine I use basically has these two lines of code in it:

To insert the values of a var, %varname% is the command I use, and this is a working regexp that I haven't been able to break to insert the data.

$line =~ s/\%([a-zA-Z0123456789\_\:]*)\%/${$1}/gi;

And to insert the values of an hash, !array{key}! is the command and here is a regexp that MAY be breakable. I use this one for selects alot ( ie <option !selectcountry{USA}!> in the html, and in the CGI program $selectcountry{$FORM{country}} = "selected"; )

$line =~ s/\!([^ ]*)\{([^ ]*)\}\!/${$1}{$2}/gi;

Please, for the love of Pete ( full pun intended ) use templates if you code CGI programs that make it into the wild.

Pete

insert into pete values('red hair','near green eyes','overinflated ego');

Replies are listed 'Best First'.
Re: Meditations on templates
by OeufMayo (Curate) on Apr 19, 2001 at 13:46 UTC

    use templates if you code CGI programs that make it into the wild

    I couldn't agree more. However, why reinvent the wheel when modules such as Template Toolkit, HTML::Template or HTML::Mason already exist and are really powerful?

    Here's a pop-up creation example with Template Toolkit:

    [% FOREACH country in country_hash %] <option value="[% country.key %]">[% country.value %]</option> [% END %]

    If you think these modules have an unaffordable overhead, you may want to use the built-in CGI HTML construction functions. In your case, you could easily create a pop-up menu like this:

    print $q->popup_menu('country_name', ['CA','NY','TN'], 'CA');

    <pedant mode="on">[a-zA-Z0123456789\_\:] can be reduced to [a-zA-Z0-9_:]<pedant mode="off">

    <kbd>--
    my $OeufMayo = new PerlMonger::Paris({http => 'paris.mongueurs.net'});</kbd>
      <pedant mode="on">[a-zA-Z0123456789\_\:] can be reduced to [a-zA-Z0-9_:]<pedant mode="off">

      And even further reduced to [\w:] :)

      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

        Not if you are using a locale, and not with Unicode support...(since we are all using pedant mode...)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-25 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found