Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How do you deal with HTML in Perl?

by blokhead (Monsignor)
on Jul 25, 2002 at 00:13 UTC ( [id://185071]=perlquestion: print w/replies, xml ) Need Help??

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

It seems like every time a write a new CGI app, I take a different approach on how to get HTML to look reasonable inside my code. I can't seem to find the perfect way to do so. I'm sure some monks out there have some great ideas I haven't thought of yet. For getting HTML to not look horrid in Perl, I've tried various things. The tag-generation code in CGI.pm is certainly OK, but that seems like it's best suited for simpler applications. The output is ugly, however I've written my own replacement to fix that. And autoescaping aside, I think print $q->table({border=>0,cellpadding=>0,cellspacing=>0,bgcolor=>"#ffffff"},$stuff) can get a little out of hand when compared to  print qq[<table border=0 cellpadding=0 cellspacing=0 bgcolor=#ffffff>$stuff</table>] (OK, so maybe I hate writing out everything into an anonymous hash) Then there's templating the HTML, which is a great way to separate it out of your Perl, except eventually the templating system must be its own programming language or else not all of your HTML stays away from the Perl. I usually need the templates to be simple so the HTML jockeys don't have to learn a scripting language. Recently I've discovered Interpolation.pm, so I can do stuff like:
print qq|<a href="blah.cgi?search=$M{param("search")}">link</a>|; # %M is tied to some mime encoding function
I'm still getting the hang of this, but it certainly doesn't solve all the nuisances of combining HTML with Perl. I'd like to know what the rest of the world does to cope with these issues ;) Thanks!

Replies are listed 'Best First'.
Re: How do you deal with HTML in Perl? (Russ: XML->XSLT?)
by Russ (Deacon) on Jul 25, 2002 at 00:29 UTC
    I now work in a heavily XML-oriented workplace. The latest idea floated around my little world is to have perl write XML (holding your data structures), then use XSLT to transform that into HTML.

    (Just completing the thought: your XSLT translation can now exist completely separately from the code, allowing a "web" developer to just deal with the output data, without needing to understand the intricacies of perl - at least that's how it was presented to me)

    • Avoids useless (expensive?) templating
    • separates data from presentation

    Not sure I'm sold on this, BTW, but it has its merits.

    The downfalls:

    • Developers must now use (and be somewhat familiar with) XML and its tools
    • "Web" developers have to be familiar with XSL as well as HTML presentation
    • If one person wears both hats (perl coder and web developer), you now have three "technologies" to understand all-at-once

    Russ
    Brainbench 'Most Valuable Professional' for Perl

      Along this line of thought, checkout AxKit. In a nutshell, it's an application framework in which you can 'bind' or map (a gross generalization) html/xml tags onto Perl modules ('taglibs'), thus keeping your logic seperated from your look and feel.

      While the designers may need to be familiar with XSLT/XSL, they also get the chance to be a programmer in ways that most WYSIWYG editors and non-programmers can deal with. I can get anyone to write <cart:add item="SKU123" quantity="1"></cart> instead of intermixing Perl and HTML/XML.

      -=Chris

Re: How do you deal with HTML in Perl?
by cjf (Parson) on Jul 25, 2002 at 01:01 UTC

    I use Here docs for anything small and templating systems for anything serious. I won't open up the CGI.pm shouldn't contain HTML generation code debate again (try a search if you're interested).

    Update: As for making heredocs look better in your code, indenting really helps:

    my $text = "Hello, World!"; print <<" EOF"; <html> <head> <title>Example</title> </head> <body> <p>$text</p> </body> </html> EOF

    Just make sure to quote the proper amount of white space before the EOF marker (also use double quotes unless you don't want variable interpolation).

Re: How do you deal with HTML in Perl?
by sedhed (Scribe) on Jul 25, 2002 at 01:22 UTC

    I use HTML::Template, and while it took a little mind-bending to get comfortable, I now swear by it. The functionality it provides is fairly basic, not hard for non-programming designers to master. Essentially:

    • Named variable interpolation (Tag: TMPL_VAR)
    • Simple IF / UNLESS <condition> variable display (TMPL_IF)
    • Repeating sections (TMPL_LOOP)
    • Sub-template includes (TMPL_INCLUDE)

    One can build apps of amazing complexity using just these basic tools. The key is that you totally seperate the data from the presentation.

    The drawback is that you have to decide where you draw the line between what is in the template's domain, and what is in the perl. For instance, I don't think a perl script should ever print table tags/elements. However, I don't put INPUT tags in my templates, preferring to use CGI.pm's methods for generating form widgets, and pass the output from those to the template variables.

    Basically, I never put HTML directly in my perl. What little HTML is generated by my perl is done in an abstract way by using modules and objects, so that repurposing would not be too difficult, and would hopefully not have to break the 'API' that my scripts use.

    To get a full and total separation, I agree with Russ, XML would be the direction to squint in. I'm not there yet myself.

    For a more complex templating system, look at Template::Toolkit. I have friends that swear by it, though I've not used it myself.

    Cheers!

Re: How do you deal with HTML in Perl?
by DamnDirtyApe (Curate) on Jul 25, 2002 at 01:05 UTC

    I have to say, I think templating is the way to go. While it's true that there may be some logic involved, the logic in your template should all be related to the presentation, and really shouldn't be too hard to grasp.

    I'm a big fan of Template Toolkit myself, and I really believe that, if your HTML jockeys are capable of writing correct HTML, they ought to be able to grasp an IF block, a FOREACH block, and an INCLUDE from another file. These are easy to do, easy to understand, and you get the abstraction you need from your code.


    _______________
    D a m n D i r t y A p e
    Home Node | Email
Re: How do you deal with HTML in Perl?
by dws (Chancellor) on Jul 25, 2002 at 01:04 UTC
    It seems like every time a write a new CGI app, I take a different approach on how to get HTML to look reasonable inside my code. I can't seem to find the perfect way to do so.

    When I run into this, I do one of three things: For really simple stuff where presentation isn't critical, I do what you've done above, and generate the HTML from CGI.pm.

    For stuff where presentation is a bit more sensitive, but I still want to keep everying in one script, I might embed the HTML after __DATA__, slurp it into a variable via   my $html = { local $/; <DATA> }; and do simple regex substitutions on it before printing it.

    More and more, I'm keeping the HTML in separate template files, and using the home-grown equivalent of HTML::Template to process it.

Re: How do you deal with HTML in Perl?
by FoxtrotUniform (Prior) on Jul 25, 2002 at 03:45 UTC

    For generating HTML, I like a combination of HTML::Template and HTML::FromText, the latter of which I nearly have a nifty patch for. Real soon now, I promise! :-)

    In general, I detest writing HTML, and would prefer to have the computer do it for me. If I want a loosely formatted document, HTML::FromText does an excellent job; otherwise, latex2html is preferred. On the other hand, neither tool produces particularly pretty markup, and I don't mind because I'm one of those archaic elitists who prefer content over eye candy. If you want a pretty site, you're probably best off embedding generated HTML in a template. (I have an example at http://www.infernus.net/; a somewhat old version of the code is listed at Automating a Static Website.)

    For parsing HTML, use HTML::Parser; for intelligently parsing pseudo-HTML, have a look at Why I like functional programming.

    --
    The hell with paco, vote for Erudil!
    :wq

Re: How do you deal with HTML in Perl?
by newrisedesigns (Curate) on Jul 25, 2002 at 01:32 UTC

    very carefully :D

    I suggest using cjf's idea concerning here documents for simple stuff and tables, and use the standard CGI stuff for subroutines and other items that might need CGI.pm printing.

    I suggest letting the HTML coder-monkeys write the template, and then form the dynamic output to the template. For example, use a here block for the header and such, then end it, branch off into a subroutine, then return to another here block.

    Hope this helps

    John J Reiser
    newrisedesigns.com

Re: How do you deal with HTML in Perl?
by trs80 (Priest) on Jul 25, 2002 at 01:10 UTC
    You might want to take a look at Text::Template as well. It allows you to use Perl inside of the template. I use it with HTML and it works good for my needs.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-16 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found