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');


In reply to Meditations on templates by petethered

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.