?? ch06_01.htm
字號(hào):
<?label 6. HTML Templates?><html><head><title>HTML Templates (CGI Programming with Perl)</title><link href="../style/style1.css" type="text/css" rel="stylesheet" /><meta name="DC.Creator" content="Scott Guelich, Gunther Birznieks and Shishir Gundavaram" /><meta scheme="MIME" content="text/xml" name="DC.Format" /><meta content="en-US" name="DC.Language" /><meta content="O'Reilly & Associates, Inc." name="DC.Publisher" /><meta scheme="ISBN" name="DC.Source" content="1565924193L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="CGI Programming with Perl" /><meta content="Text.Monograph" name="DC.Type" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" alt="Book Home" usemap="#banner-map" border="0" /><map name="banner-map"><area alt="CGI Programming with Perl" href="index.htm" coords="0,0,466,65" shape="rect" /><area alt="Search this book" href="jobjects/fsearch.htm" coords="467,0,514,18" shape="rect" /></map><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch05_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm">CGI Programming with Perl</a></td><td width="172" valign="top" align="right"><a href="ch06_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><hr align="left" width="515" /><h1 class="chapter">Chapter 6. HTML Templates</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch06_01.htm">Reasons for Using Templates</a><br><a href="ch06_02.htm">Server Side Includes</a><br><a href="ch06_03.htm">HTML::Template</a><br><a href="ch06_04.htm">Embperl</a><br><a href="ch06_05.htm">Mason</a><br></p></div><p>The CGI.pm module makes it much easier to produce <a name="INDEX-1237" /> <a name="INDEX-1,238" />HTML code from CGI scripts written inPerl. If your goal is to produce self-contained CGI applications thatinclude both the program logic and the interface (HTML), then CGI.pmis certainly the best tool for this. It excels for distributableapplications because you do not need to distribute separate HTMLfiles, and it's easy for developers to follow when readingthrough code. For this reason, we use it in the majority of theexamples in this book. However, in some circumstances, there are goodreasons for separating the interface from the program logic. In thesecircumstances, templates may be a better solution.</p><div class="sect1"><a name="ch06-95555" /><h2 class="sect1">6.1. Reasons for Using Templates</h2><p><a name="INDEX-1239" /> <a name="INDEX-1,240" />HTML design and CGI development involvevery different skill sets. Good HTML design is typically done byartists or designers in collaboration with marketing folks and peopleskilled in interface design. CGI development may also involve inputfrom others, but it is very technical in nature. Therefore, CGIdevelopers are often not responsible for creating the interface totheir applications. In fact, sometimes they are given non-functionalprototypes and asked to provide the logic to drive it. In thisscenario, the HTML is already available and translating it into codeinvolves extra work.</p><p>Additionally, CGI applications rarely remain static; they requiremaintenance. Inevitably, bugs are found and fixed, new features areadded, the wording is changed, or the site is redesigned with a newcolor scheme. These changes can involve either the program logic orthe interface, but interface changes are often the most common andthe most time consuming. Making specific changes to an existing HTMLfile is generally easier than modifying a CGI script, and manyorganizations have more people who understand HTML than whounderstand Perl.</p><p>There are many different ways to use HTML templates, and it is verycommon for web developers to create their own custom solutions.However, the many various solutions can be grouped into a fewdifferent approaches. In this chapter, we'll explore eachapproach by looking at the most powerful and popular solutions foreach.</p><a name="ch06-1-fm2xml" /><div class="sect2"><h3 class="sect2">6.1.1. Rolling Your Own</h3><p>One thing we won't do in this chapter is present a noveltemplate parser or explain how to write your own. The reason is thatthere are already too many good solutions to warrant this. Of themany web developers out there who have created their own proprietarysystems for handling templates, most turn to something else aftersome time. In fact, one of your authors has experience doing justthis.</p><p>The first custom template system I developed was like SSI but withcontrol structures added as well as the ability to nest multiplecommands in parentheses (commands resembled Excel functions). Thetemplate commands were simple, powerful, and efficient, but theunderlying code was complicated and difficult to maintain, so at onepoint I started over. My second solution included a hand-coded,recursive descent parser and an object-oriented, JavaScript-likesyntax that was easily extended in Perl. My thinking was that manyHTML authors were comfortable with JavaScript already. I was ratherproud of it when it was finished, but after a few months of using it,I realized I had created an over-engineered, proprietary solution,and I ported the project to Embperl.</p><p>In both of my attempts, I realized the solutions were not worth theeffort required to maintain them. In the second case, the code wasvery maintainable, but even minor maintenance did not seem worth theeffort given the high-quality, open source alternatives that arealready tested, maintained, and available for all to use. Moreimportantly, custom solutions require other developers and HTMLauthors to invest time learning systems that they would neverencounter elsewhere. No one told me I had to choose a standardsolution over a proprietary one, but I discovered the advantages onmy own. Sometimes ego must yield to practicality.</p><p>So consider the options that are already available and avoid the urgeto reinvent the wheel. If you need a particular feature that is notavailable in another package, consider extending an existing opensource solution and give your code back if you think it will benefitothers. Of course, in the end what you do is up to you, and you mayhave a good reason for creating your own solution. You could evenpoint out that none of the solutions presented in this chapter wouldexist if a few people hadn't decided they should create theirown respective solutions, maintain and extend them, and make themavailable to others.</p></div></div><hr align="left" width="515" /><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch05_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td width="172" valign="top" align="right"><a href="ch06_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">5.5. Handling Errors</td><td width="171" valign="top" align="center"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td width="172" valign="top" align="right">6.2. Server Side Includes</td></tr></table></div><hr align="left" width="515" /><img src="../gifs/navbar.gif" alt="Library Navigation Links" usemap="#library-map" border="0" /><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area href="../index.htm" coords="1,1,83,102" shape="rect" /><area href="../lnut/index.htm" coords="81,0,152,95" shape="rect" /><area href="../run/index.htm" coords="172,2,252,105" shape="rect" /><area href="../apache/index.htm" coords="238,2,334,95" shape="rect" /><area href="../sql/index.htm" coords="336,0,412,104" shape="rect" /><area href="../dbi/index.htm" coords="415,0,507,101" shape="rect" /><area href="../cgi/index.htm" coords="511,0,601,99" shape="rect" /></map></body></html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -