Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Separation of content and code

by tinman (Curate)
on Jun 18, 2001 at 12:22 UTC ( [id://89244]=note: print w/replies, xml ) Need Help??


in reply to Separation of content and code

Well, I think your question is a little unclear to me, but I'll try and do a spot of guesswork and shoot in a few directions... just a reminder: try to preview before you submit a question, a bit of punctuation and a few paragraph <p> tags here and there could help a lot

If I understand correctly, your scenario is that you've written a CGI script, which generates HTML (using print statements or heredocs)..

Now this posting is a great place to start reading on why you need to put your HTML in a different file.. quite simply, you want to separate your code from your presentation (HTML) because that makes it easier to maintain... if someone asks you to do minor changes to the HTML, then you need to dig through all of your code to find out the place to change, and you could actually break some of your code in the process...

Another thing, if you separate your HTML into another file, someone who doesn't know Perl well, but who does know HTML could actually figure out what to do, and fix it..

Another option that I think you mention is to generate an HTML file and display that on the webserver, rather than a CGI script.. (correct me if I didn't guess right).. well, you could do this, but then your content would no longer be dynamic, it would have to rely on your CGI script executing periodically and regenerating your HTML file.. if you need to display something that doesn't change very often, or if it does not need to be customized by individual viewers of your site (for example, a site like Perlmonks has to be customized and dynamic because different users have different views of the content on the site), then generating static HTML pages will mean that the webserver doesn't have a lot of overhead running a CGI script..

I hope this answered your question.. if you need any clarification or if I didn't answer what you needed to know, please reply on this thread...

Replies are listed 'Best First'.
Re: Re: Separation of content and code
by sierrathedog04 (Hermit) on Jun 18, 2001 at 13:28 UTC
    OTOH, CGI.pm has a lot of commands for writing tables and other HTML, so a lot of people including me must be mixing their program logic and their HTML in the same file.

    What if the structure of the HTML depends upon the program logic? I may for instance show data as a table if more than one row is returned, but as a paragraph otherwise.

    I do not believe that it is necessarily better to use templates. Merlyn has an entire chapter on CGI.pm in the 2nd version of Learning Perl, and I don't recall him giving such advice.

      Most template solutions have minimal built in logic to differ output based on program input. (or least I know that Template Toolkit does for sure). However, I only do this when the change is subtle with the overall page keeping much of the same layout; insted it's usually easier to write two different template files if the change is more drastic. Note that still cleans up your perl code, more so than writing two branches of CGI.pm HTML functions.

      But templates are not always the solution in every case. If you have a SSI included script, for example, you only need the minimal HTML that CGI provides. Short, demonstration scripts that aren't really part of a site could easily qualify as well. In addition, if you are building a page that has to be speedy (maybe it's your home page of a million+ hits per day site), a template solution could just slow you down, though if the rest of the site is in template form, it's a tossup. (In general in this situation, your 'entrance' page ought to be statically delievered, which might mean you need to create it every 5 minutes or so from dynamic content).

      But if you are building a site of any magnitue more than a handful of pages, you really ought to be considering templates as to avoid problems with consistent site look after a time.


      Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-20 12:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found