⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch06_03.htm

📁 用perl编写CGI的好书。本书从解释CGI和底层HTTP协议如何工作开始
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<?label 6.3. HTML::Template?><html><head><title>HTML::Template (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="ch06_02.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_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><hr align="left" width="515" /><h2 class="sect1">6.3. HTML::Template</h2><p>SSI is quite powerful, but it does have limitations. Its advantagesare that it is efficient and simple enough for HTML designers withoutprogramming experience to use. The disadvantages are that it only hasa handful of commands, and it only parses static documents.<a name="INDEX-1351" /> <a name="INDEX-1,352" /><a name="INDEX-1353" /> <a name="INDEX-1,354" />HTML::Templateis a simple template parser that addresses both of these issues whilestill maintaining a simple interface.</p><a name="ch06-5-fm2xml" /><div class="sect2"><h3 class="sect2">6.3.1. Syntax</h3><p><a name="INDEX-1355" /><a name="INDEX-1356" />HTML::Templateactually has fewer commands than SSI, but because the value of itsvariable tags can be set to anything by a CGI script, it is moreflexible. While it's true that an SSI document can include CGIoutput, this becomes unwieldy if a page includes several complexcomponents that must each execute a CGI script. HTML::Templatesupports complex templates with the execution of a single CGI script.</p><p>Let's look at a very simple example that displays the current<a name="INDEX-1357" />date and time. <a href="ch06_03.htm#ch06-63459">Example 6-3</a> shows the template file.</p><a name="ch06-63459" /><div class="example"><h4 class="objtitle">Example 6-3. current_time.tmpl </h4><a name="INDEX-1358" /><blockquote><pre class="code">&lt;HTML&gt;&lt;HEAD&gt;  &lt;TITLE&gt;Current Time&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY BGCOLOR="white"&gt;  &lt;H1&gt;Current Time&lt;/H1&gt;  &lt;P&gt;Welcome. The current time is &lt;TMPL_VAR NAME="current_time"&gt;.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</pre></blockquote></div><p>This is a standard HTML file with one added tag:<a name="INDEX-1359" />&lt;TMPL_VAR NAME="current_date" &gt;.<a name="INDEX-1360" /><a name="INDEX-1361" />HTML::Template'scommands can be formatted like standard HTML tags or as comments. Thefollowing is also acceptable:</p><blockquote><pre class="code">&lt;!-- TMPL_VAR NAME="current_date" --&gt;</pre></blockquote><p>This alternate syntax makes the commands easier to input into HTMLeditors that may be restrictive about the tags they allow. In orderto use this template, we must create a CGI script that is the targetof the request. The code for this is shown in <a href="ch06_03.htm#ch06-60742">Example 6-4</a>.</p><a name="ch06-60742" /><div class="example"><h4 class="objtitle">Example 6-4. current_time.cgi </h4><a name="INDEX-1362" /><a name="INDEX-1,363" /><blockquote><pre class="code">#!/usr/bin/perl -wTuse strict;use HTML::Template;use constant TMPL_FILE =&gt; "$ENV{DOCUMENT_ROOT}/templates/current_time.tmpl";my $tmpl = new HTML::Template( filename =&gt; TMPL_FILE );my $time = localtime;$tmpl-&gt;param( current_time =&gt; $time );print "Content-type: text/html\n\n",      $tmpl-&gt;output;</pre></blockquote></div><p>We create a constant called <tt class="literal">TMPL_FILE</tt> that pointsto the template file we will use. We then create an HTML::Templateobject, assign a parameter, and output it. Most tags have a<a name="INDEX-1364" /><a name="INDEX-1365" />NAMEattribute; this value of this attribute corresponds to a parameterset by a CGI script via <a name="INDEX-1366" />HTML::Template's<tt class="function">param</tt> method, which (by design) works much likeCGI.pm's <tt class="function">param</tt><a name="INDEX-1367" /> method. In fact, you can importparameters from CGI.pm when you create a HTML::Template<a name="INDEX-1368" />object:</p><blockquote><pre class="code">my $q    = new CGI;my $tmpl = new HTML::Template( filename  =&gt; TMPL_FILE,                               associate =&gt; $q );</pre></blockquote><p>This loads all of the form parameters that your CGI script justreceived; you can of course still use the <tt class="function">param</tt>method to add additional parameters or override those loaded fromCGI.pm.</p><p>HTML::Template's<a name="INDEX-1369" /> <a name="INDEX-1,370" />commands are summarized in <a href="ch06_03.htm#ch06-78999">Table 6-4</a>.</p><a name="ch06-78999" /><h4 class="objtitle">Table 6-4. Commands Available in HTML::Template </h4><table border="1"><tr><th><p>Element</p></th><th><p>Attribute</p></th><th><p>Description</p></th></tr><tr><td><p><em class="filename">TMPL_VAR</em></p></td><td><p><em class="filename">NAME="param_name"</em></p></td><td><p>This tag is replaced by the value of the parameter<em class="emphasis">param_name</em>; has no closing tag.</p></td></tr><tr><td /><td><p><em class="filename">ESCAPE="HTML|URL"</em></p></td><td><p>If this is set to "HTML", then the value substituted forthis tag is HTML escaped (e.g., <tt class="literal">"</tt> will be replacedby <tt class="literal">&amp;quot;</tt>, etc.); "URL" willencode the value for URLs. No escaping is done if this is set toor omitted.</p></td></tr><tr><td><p><em class="filename">TMPL_LOOP</em></p></td><td><p><em class="filename">NAME="param_name"</em></p></td><td><p>Loops over content between its opening and closing tags for each itemin the array that corresponds to <em class="emphasis">param_name</em>, seebelow.</p></td></tr><tr><td><p><em class="filename">TMPL_IF</em></p></td><td><p><em class="filename">NAME="param_name"</em></p></td><td><p>Content within this tag is omitted unless the parameter<em class="emphasis">param_name</em> is true.</p></td></tr><tr><td><p><em class="filename">TMPL_ELSE</em></p></td><td /><td><p>This reverses the condition for the remaining content within a<em class="emphasis">TMPL_IF</em> or <em class="emphasis">TMPL_UNLESS</em> tag.</p></td></tr><tr><td><p><em class="filename">TMPL_UNLESS</em></p></td><td><p><em class="filename">NAME="param_name"</em></p></td><td><p>The reverse of <em class="emphasis">TMPL_IF</em>. Content within this tagis omitted unless the parameter param_name is false.</p></td></tr><tr><td><p><em class="filename">TMPL_INCLUDE</em></p></td><td><p><em class="filename">NAME="/file/path"</em></p></td><td><p>Includes the contents of another file; has no closing tag.</p></td></tr></table><p>Only the <a name="INDEX-1371" /> <a name="INDEX-1,372" /><a name="INDEX-1373" />TMPL_LOOP, TMPL_IF, andTMPL_UNLESS commands have opening and closing tags; the others aresingle tags (like &lt;HR&gt; or &lt;BR&gt;).</p><a name="ch06-6-fm2xml" /><div class="sect3"><h3 class="sect3">6.3.1.1. Loops</h3><p>One of the most convenient features that<a name="INDEX-1374" /><a name="INDEX-1375" /> <a name="INDEX-1,376" />HTML::Templateoffers is the ability to create loops. The previous exampledidn't take advantage of this, so let's look at a morecomplex example. HTML::Template requires an<a name="INDEX-1377" />array of hashes forloops. It loops over each element in the array and creates variablescorresponding to the hash keys. You can visualize this structure as atable, as in <a href="ch06_03.htm#ch06-95790">Table 6-5</a>, which can be representedin Perl as an array of hashes, as in <a href="ch06_03.htm#ch06-86484">Example 6-5</a>.</p><a name="ch06-95790" /><h4 class="objtitle">Table 6-5. A Sample Table of Data </h4><table border="1"><tr><th><p>Name</p></th><th><p>Location</p></th><th><p>Age</p></th></tr><tr><td><p>Mary</p></td><td><p>Minneapolis</p></td><td><p>37</p></td></tr><tr><td><p>Fred</p></td><td><p>Chicago</p></td><td><p>24</p></td></tr><tr><td><p>Martha</p></td><td><p>Orlando</p></td><td><p>51</p></td></tr><tr><td><p>Betty</p></td><td><p>Los Angeles</p></td><td><p>19</p></td></tr><tr><td><p>...</p></td><td><p>...</p></td><td><p>...</p></td></tr></table><a name="ch06-86484" /><div class="example"><h4 class="objtitle">Example 6-5. A Perl Data Structure Corresponding to <a href="ch06_03.htm#ch06-95790">Table 6-5</a> </h4><blockquote><pre class="code">@table = (    { name     =&gt; "Mary",      location =&gt; "Minneapolis",      age      =&gt; "37" },    { name     =&gt; "Fred",      location =&gt; "Chicago",      age      =&gt; "24" },

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -