Template Toolkit : Perl http://forum.template-toolkit.ru/view_forum/forum_id-8.html?rss Template Toolkit - , . , . Text::Template, HTML::Template, Mason ru Lobanov Igor Lobanov Igor <webmaster@template-toolkit.ru> Thu, 17 Mar 2005 12:03:32 GMT Wed, 4 Jan 2006 02:29:01 GMT Perl script, Template Toolkit http://forum.template-toolkit.ru/images/tt2powered.gif Template Toolkit - perl http://forum.template-toolkit.ru/ 88 31 http://blogs.law.harvard.edu/tech/rss http://forum.template-toolkit.ru/view_topic/topic_id-53.html?rss Tue, 28 Dec 2004 19:07:19 GMT , , http://forum.template-toolkit.ru/view_topic/topic_id-53.html?rss <a target="_blank" href="http://www.theperlreview.com/">Perl Review</a> (brian d foy) &quot;Separating Code, Presentation, and Configuration&quot;. .<br /><br /><span style="font-weight:bold"> .</span><br /><br /> , , .<br /><br /><span style="font-weight:bold">1. .</span><br /><br /> , , Rich Site Summaries (RSS) -<span style="font-weight:bold"><sup>1</sup></span>. , , .<br /><br /><span style="font-style:italic"> 1</span> , . <span style="font-weight:bold">@files</span> , , <span style="font-weight:bold">$base</span> - , , <span style="font-weight:bold">print</span> HTML ( HTML- CGI). . , , - .<br /><br /><span style="font-style:italic"> 1: RSS .</span><br /><div class="code"><pre>1 #!/usr/bin/perl -w 2 use strict; 3 4 use LWP::Simple; 5 use XML::RSS; 6 7 my @files = qw( 8 http://use.perl.org/useperl.rss 9 http://search.cpan.org/rss/search.rss 10 http://jobs.perl.org/rss/standard.rss 11 http://www.perl.com/pace/perlnews.rdf 12 http://www.perlfoundation.org/perl-foundation.rdf 13 http://www.stonehenge.com/merlyn/UnixReview/ur.rss 14 http://www.stonehenge.com/merlyn/WebTechniques/wt.rss 15 http://www.stonehenge.com/merlyn/LinuxMag/lm.rss 16 ); 17 18 my $base = '/usr/home/comdog/TPR/rss-html'; 19 20 foreach my $url ( @files ) 21 { 22 my $file = $url; 23 24 $file =~ s|.*/||; 25 26 my $result = open my $fh, &quot;&gt; $base/$file.html&quot;; 27 28 unless( $result ) 29 { 30 warn &quot;Could not open [$file] for writing! $!&quot;; 31 next; 32 } 33 34 select $fh; 35 36 my $rss = XML::RSS-&gt;new(); 37 my $data = get( $url ); 38 $rss-&gt;parse( $data ); 39 40 my $channel = $rss-&gt;{channel}; 41 my $image = $rss-&gt;{image}; 42 43 print &lt;&lt;&quot;HTML&quot;; 44 &lt;table cellpadding=1&gt;&lt;tr&gt;&lt;td bgcolor=&quot;#000000&quot;&gt; 45 &lt;table cellpadding=5&gt; 46 &lt;tr&gt;&lt;td bgcolor=&quot;#aaaaaa&quot; align=&quot;center&quot;&gt; 47 HTML 48 49 if( $image-&gt;{url} ) 50 { 51 my $img = qq|&lt;img src=&quot;$$image{url}&quot; alt=&quot;$$channel{title}&quot;&gt;|; 52 print qq|&lt;a href=&quot;$$channel{link}&quot;&gt;$img&lt;/a&gt;&lt;br&gt;\n|; 53 } 54 else 55 { 56 print qq|&lt;a href=&quot;$$channel{link}&quot;&gt;$$channel{title}&lt;/a&gt;&lt;br&gt;\n|; 57 } 58 59 print &lt;&lt;&quot;HTML&quot;; 60 &lt;font size=&quot;-1&quot;&gt;$$channel{description}&lt;/font&gt; 61 &lt;/td&gt;&lt;/tr&gt; 62 &lt;tr&gt;&lt;td bgcolor=&quot;#bbbbff&quot; width=200&gt;&lt;font size=&quot;-1&quot;&gt; 63 HTML 64 65 foreach my $item ( @{ $rss-&gt;{items} } ) 66 { 67 print qq|&lt;b&gt;&gt;&lt;/b&gt;&lt;a href=&quot;$$item{link}&quot;&gt;$$item{title}&lt;/a&gt;&lt;br&gt;&lt;br&gt;\n|; 68 } 69 70 print &lt;&lt;&quot;HTML&quot;; 71 &lt;/font&gt;&lt;/td&gt;&lt;/tr&gt; 72 &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt; 73 &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt; 74 HTML 75 76 close $fh; 77 }</pre></div><br /><br /><span style="font-weight:bold">2. .</span><br /><br /> . -, - , RSS . HTML, TeX, , - , .<br /><br /> - , Text::Template - (Mark-Jason Dominus). , Perl. , Perl.<br /><br /><span style="font-style:italic"> 2</span> - , HTML Text::Template. 5 fill_in_file(). 13 , . HTML, , , <span style="font-style:italic"> 3</span>.<br /><br /> Text::Template . , - , . , . - , - , .<br /><br />, XML::RSS - . , . , . <span style="font-weight:bold">$rss-&gt;channel</span>, , <span style="font-weight:bold">%channel</span>, <span style="font-weight:bold">$rss-&gt;items</span>, <span style="font-weight:bold">@items</span>.<br /><br /><span style="font-style:italic"> 2: .</span><br /><div class="code"><pre>1 #!/usr/bin/perl -w 2 use strict; 3 4 use LWP::Simple; 5 use Text::Template qw(fill_in_file); 6 use XML::RSS; 7 8 my @files = qw( 9 http://use.perl.org/useperl.rss 10 ); 11 12 my $base = '.'; 13 my $template = 'rss-html.tmpl'; 14 15 foreach my $url ( @files ) 16 { 17 my $file = $url; 18 19 $file =~ s|.*/||; 20 21 my $result = open my $fh, &quot;&gt; $base/$file.html&quot;; 22 23 unless( $result ) 24 { 25 warn &quot;Could not open [$file] for writing! $!&quot;; 26 next; 27 } 28 29 my $rss = XML::RSS-&gt;new(); 30 my $data = get( $url ); 31 $rss-&gt;parse( $data ); 32 33 print fill_in_file( $template, HASH =&gt; $rss ); 34 close $fh; 35 }</pre></div><br /><br /> Text::Template , . . - , fill_in_file() , <span style="font-style:italic"> 2</span>.<br /><br /><span style="font-style:italic"> 3: HTML .</span><br /><div class="code"><pre>1 &lt;table cellpadding=1&gt;&lt;tr&gt;&lt;td bgcolor=&quot;#000000&quot;&gt; 2 &lt;table cellpadding=5&gt; 3 &lt;tr&gt; 4 &lt;td bgcolor=&quot;#aaaaaa&quot; align=&quot;center&quot;&gt; 5 &lt;a href=&quot;{ $channel{link} }&quot;&gt;{ 6 7 $image ? qq|&lt;img src=&quot;$image&quot; alt=&quot;$channel{title}&quot;&gt;| : $channel{title} 8 9 }&lt;/a&gt;&lt;br&gt; 10 11 { $channel{description} } 12 &lt;/td&gt; 13 &lt;/tr&gt; 14 15 &lt;tr&gt; 16 &lt;td bgcolor=&quot;#bbbbff&quot; width=200&gt;&lt;font size=&quot;-1&quot;&gt; 17 { 18 my $str; 19 20 foreach my $item ( @items ) 21 { 22 $str .= qq|&lt;b&gt;&gt;&lt;/b&gt;&lt;a href=&quot;$$item{link}&quot;&gt;$$item{title}&lt;/a&gt;&lt;br&gt;&lt;br&gt;\n|; 23 } 24 25 $str; 26 }&lt;/font&gt;&lt;/td&gt; 27 &lt;/tr&gt; 28 &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt; 29 &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</pre></div><br /><br /> , . , . HTML , , <span style="font-style:italic"> 4</span>.<br /><br /><span style="font-style:italic"> 4: .</span><br /><div class="code"><pre>1 { $channel{title} } 2 3 { $channel{description} } 4 5 { 6 my $str; 7 8 foreach my $item ( @items ) 9 { 10 $str .= qq|* $$item{title}\n|; 11 } 12 13 $str; 14 }</pre></div><br /><br /><span style="font-weight:bold">3. .</span><br /><br /> . <span style="font-style:italic"> 1</span> , - , . <span style="font-style:italic"> 2</span> , , . .<br /><br /> - , , , . , - . , .<br /><br /> . Comprehensive Perl Archive Network (CPAN)<span style="font-weight:bold"><sup>2</sup></span> . , .<br /><br /> , CPAN ConfigReader::Simple, -. , (Bek Oberin), , .<br /><br /><span style="font-style:italic"> 5</span> <span style="font-style:italic"> 2</span> ConfigReader::Simple. , . ( , Perl get()). <span style="font-style:italic"> 6</span> .<br /><br /><span style="font-style:italic"> 5: ConfigReader::Simple.</span><br /><div class="code"><pre>1 #!/usr/bin/perl -w 2 use strict; 3 4 use ConfigReader::Simple; 5 use LWP::Simple; 6 use Text::Template qw(fill_in_file); 7 use XML::RSS; 8 9 my $config = ConfigReader::Simple-&gt;new( './rss.config' ); 10 11 my $base = $config-&gt;base; 12 my $template = $config-&gt;template; 13 my $extension = $config-&gt;extension; 14 15 my @files = split /\s+/, $config-&gt;files; 16 17 foreach my $url ( @files ) 18 { 19 my $file = $url; 20 21 $file =~ s|.*/||; 22 23 my $result = open my $fh, &quot;&gt; $base/$file.$extension&quot;; 24 25 unless( $result ) 26 { 27 warn &quot;Could not open [$file] for writing! $!&quot;; 28 next; 29 } 30 31 my $rss = XML::RSS-&gt;new(); 32 my $data = get( $url ); 33 $rss-&gt;parse( $data ); 34 35 print $fh fill_in_file( $template, HASH =&gt; $rss ); 36 close $fh; 37 }</pre></div><br /><span style="font-style:italic"> 6: .</span><br /><div class="code"><pre>1 base . 2 template rss-html.tmpl 3 files http://use.perl.org/useperl.rss 4 extension html</pre></div><br /><br /><span style="font-weight:bold">4. .</span><br /><br /> , . . , . Text::Template ConfigReader::Simple , .<br /><br /><span style="font-weight:bold">5. </span><br /><br /> , , Comprehensive Perl Archive Network (CPAN) - <a target="_blank" href="http://search.cpan.org">http://search.cpan.org</a><br /><br /><span style="font-weight:bold">6. .</span><br /><br /> (brian d foy) - <span style="font-style:italic">The Perl Review</span>.<br /><br /><span style="font-weight:bold">.</span><br /><br /><span style="font-weight:bold"><sup>1</sup></span> &quot;Simple RSS with Perl&quot; by brian d foy, The Perl Review v0 i5, November 2002, <a target="_blank" href="http://www.theperlreview.com">http://www.ThePerlReview.com</a><br /><br /><span style="font-weight:bold"><sup>2</sup></span> <a target="_blank" href="http://search.cpan.org">http://search.cpan.org</a><br /><br /><a target="_blank" href="http://www.theperlreview.com/Articles/v0i7/config.pdf"> Perl Review (PDF)</a> <authors@template-toolkit.ru> http://forum.template-toolkit.ru/view_topic/topic_id-53.html , Perl http://forum.template-toolkit.ru/view_topic/topic_id-53.html?rss