用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - perl代码库

创建HTML模板

2012-10-15 作者: 神马举报

[perl]代码库

#-----------------------------
sub template
{
	my ( $filename, $fillings ) = @_;
	my $text;
	local $/;
# slurp mode (undef)
	local *F;
# create local filehandle
	open ( F, "< $filename\0" )    || return;
	$text = <F>;
# read whole file
	close ( F );
# ignore retval
# replace quoted words with value in %$fillings hash
	$text =~ s{ %% ( .*? ) %% }
	{ exists ( $fillings->{$1} )
		? $fillings->{$1}
		: ""
	} gsex;
	return $text;
}
#-----------------------------
#<!-- simple.template for internal template() function -->
#<HTML><HEAD><TITLE>Report for %%username%%</TITLE></HEAD>
#<BODY><H1>Report for %%username%%</H1>
#%%username%% logged in %%count%% times, for a total of %%total%% minutes.
#-----------------------------
#<!-- fancy.template for Text::Template -->
#<HTML><HEAD><TITLE>Report for {$user}</TITLE></HEAD>
#<BODY><H1>Report for {$user}</H1>
#{ lcfirst($user) } logged in {$count} times, for a total of
#{ int($total / 60) } minutes.
#-----------------------------
%fields = (
              username => $whats_his_name,
              count    => $login_count,
              total    => $minute_used,
          );

print template ( "/home/httpd/templates/simple.template", \%fields );
#-----------------------------
# download the following standalone program
#!/usr/bin/perl -w
# userrep1 - report duration of user logins using SQL database

use DBI;
use CGI qw ( :standard );

# template() defined as in the Solution section above
$user = param ( "username" )                   or die "No username";

$dbh = DBI->connect ( "dbi:mysql:connections:mysql.domain.com:3306",
                      "connections", "seekritpassword" )       or die "Couldn't connect\n";
$sth = $dbh->prepare ( <<"END_OF_SELECT" )     or die "Couldn't prepare SQL";
SELECT COUNT ( duration ),SUM ( duration )
FROM logins WHERE username='$user'
END_OF_SELECT

# this time the duration is assumed to be in seconds
if ( @row = $sth->fetchrow() )
{
	( $count, $seconds ) = @row;
}
else
{
	( $count, $seconds ) = ( 0,0 );
}

$sth->finish();
$dbh->disconnect;

print header();
print template ( "report.tpl",
{
	'username' => $user,
	'count'    => $count,
	'total'    => $total
} );

#-----------------------------
You owe:
{$total}
#-----------------------------
The average was {$count ?  ( $total/$count ) : 0} .
#-----------------------------
# download the following standalone program
#!/usr/bin/perl -w
# userrep2 - report duration of user logins using SQL database

use Text::Template;
use DBI;
use CGI qw ( :standard );

$tmpl = "/home/httpd/templates/fancy.template";
$template = Text::Template->new ( -type => "file", -source => $tmpl );
$user = param ( "username" )                   or die "No username";

$dbh = DBI->connect ( "dbi:mysql:connections:mysql.domain.com:3306",
                      "connections", "secret passwd" )         or die "Couldn't db connect\n";
$sth = $dbh->prepare ( <<"END_OF_SELECT" )     or die "Couldn't prepare SQL";
SELECT COUNT ( duration ),SUM ( duration )
FROM logins WHERE username='$user'
                           END_OF_SELECT

                           $sth->execute()                             or die "Couldn't execute SQL";

if ( @row = $sth->fetchrow() )
{
	( $count, $total ) = @row;
}
else
{
	$count = $total = 0;
}

$sth->finish();
$dbh->disconnect;

print header();
print $template->fill_in();

#-----------------------------


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...