Common Lisp的人有他们的CL-WHO,它使HTML模板与"主要"语言集成,从而使任务更容易.对于那些不了解CL-WHO的人来说,它看起来像这样(例如来自CL-WHO的网页):
(with-html-output (*http-stream*) (:table :border 0 :cellpadding 4 (loop for i below 25 by 5 do (htm (:tr :align "right" (loop for j from i below (+ i 5) do (htm (:td :bgcolor (if (oddp j) "pink" "green") (fmt "~@R" (1+ j))))))))))
你知道其他语言的这样的图书馆吗?我所知道的(模仿CL-WHO)是BrevéforPython.我对Perl风格特别感兴趣,但有趣的是其他语言如何处理将HTML集成到他们的语法中.
对于CPAN产品,请查看以下内容(按字母顺序排列)......
生成器
HTML :: AsSubs
HTML ::微小
Markapl
模板::声明
XML ::发电机
使用CL-WHO示例提供的表格部分(减去罗马数字和s /背景颜色/颜色/将代码压缩到屏幕宽度!)....
use Builder; my $builder = Builder->new; my $h = $builder->block( 'Builder::XML' ); $h->table( { border => 0, cellpadding => 4 }, sub { for ( my $i = 1; $i < 25; $i += 5 ) { $h->tr( { align => 'right' }, sub { for my $j (0..4) { $h->td( { color => $j % 2 ? 'pink' : 'green' }, $i + $j ); } }); } }); say $builder->render;
use HTML::AsSubs; my $td = sub { my $i = shift; return map { td( { color => $_ % 2 ? 'pink' : 'green' }, $i + $_ ) } 0..4; }; say table( { border => 0, cellpadding => 4 }, map { &tr( { align => 'right' }, $td->( $_ ) ) } loop( below => 25, by => 5 ) )->as_HTML;
use HTML::Tiny; my $h = HTML::Tiny->new; my $td = sub { my $i = shift; return map { $h->td( { 'color' => $_ % 2 ? 'pink' : 'green' }, $i + $_ ) } 0..4; }; say $h->table( { border => 0, cellpadding => 4 }, [ map { $h->tr( { align => 'right' }, [ $td->( $_ ) ] ) } loop( below => 25, by => 5 ) ] );
use Markapl; template 'MyTable' => sub { table ( border => 0, cellpadding => 4 ) { for ( my $i = 1; $i < 25; $i += 5 ) { row ( align => 'right' ) { for my $j ( 0.. 4 ) { td ( color => $j % 2 ? 'pink' : 'green' ) { $i + $j } } } } } }; print main->render( 'MyTable' );
package MyTemplates; use Template::Declare::Tags; use base 'Template::Declare'; template 'MyTable' => sub { table { attr { border => 0, cellpadding => 4 }; for ( my $i = 1; $i < 25; $i += 5 ) { row { attr { align => 'right' }; for my $j ( 0..4 ) { cell { attr { color => $j % 2 ? 'pink' : 'green' } outs $i + $j; } } } } } }; package main; use Template::Declare; Template::Declare->init( roots => ['MyTemplates'] ); print Template::Declare->show( 'MyTable' );
use XML::Generator; my $x = XML::Generator->new( pretty => 2 ); my $td = sub { my $i = shift; return map { $x->td( { 'color' => $_ % 2 ? 'pink' : 'green' }, $i + $_ ) } 0..4; }; say $x->table( { border => 0, cellpadding => 4 }, map { $x->tr( { align => 'right' }, $td->( $_ ) ) } loop( below => 25, by => 5 ) );
以下内容可用于在HTML :: AsSubs/HTML :: Tiny/XML :: Generator示例中生成"循环"....
sub loop { my ( %p ) = @_; my @list; for ( my $i = $p{start} || 1; $i < $p{below}; $i += $p{by} ) { push @list, $i; } return @list; }
一个Perl的基金会目前的赠款资助项目(一个对Perl 6轻量级的Web框架)的工作Perl6代码提供了一个类似的界面:
use Tags; say show { html { head { title { 'Tags Demo' } } body { outs "hi"; ul :id{ outs "A list from one to ten:"; for 1..10 { li :class , { $_ } } } } } }
浏览或克隆github上的当前代码.
Perl的CGI模块支持这样的东西.
use CGI ':standard'; use Lisp::Fmt print header(); print table( { -border => 1, -cellpading => 4}, loop({ below => 25, by=> 5}, sub { my $i = shift; tr( {-align => 'right'} , loop({ from => $i, below $i + 5}, sub { my $j = shift; td({-bgcolor => ($oddp eq $j ? 'pink' : 'green')} fmt("~@R", 1+$j); }) ) });
我试图保持lispy,所以你必须loop
自己实现lispy 功能.我没有真正编写Common List,所以我希望我能正确理解你的代码.