这是使用Template Toolkit(“TT”)构建 HTML 页面并包含来自 JSON 的数据的重写
您的原始 HTML 存在许多问题。标签是不平衡的,并且您在正文中只有头部元素。我不能保证这可以解决所有问题,但我不能再发现任何错误。您应该小心保持 HTML 缩进,以便更容易发现不平衡的标签。它还消除了注释结束标签的需要
我包含了一段代码,向您展示了如何更轻松地从每个哈希列表中提取相同字段的数组。没有用到变量,所以请在理解课程后删除该代码
模板工具包的模板通常保存在单独的.tt 文件中。在这种情况下,我使用了DATA 文件句柄,因为它更符合您编写原始代码的方式。但请记住,TT 模板可能包含引用其他单独文件的include 语句。例如,您可以将页脚部分放在外部文件中,然后将 [% INCLUDE footer.tt %] 放在主模板中。但那是为了未来
请注意,模板工具包允许您直接访问 Perl 数据结构。模板需要使用的任何东西都必须在process 方法调用的第二个参数中传递。在这种情况下,我已经传递了{ json => $decoded_json },以便TT 现在可以使用json 标识符来引用您下载的哈希。在模板中,json.items 现在是数据数组(可作为json.items.0、json.items.1 等访问),在这里我写了[% FOREACH item IN json.items %],它声明了一个新的TT 变量item 并将其分配给items 数组依次
我希望所有这些都清楚
#!/usr/bin/perl
use utf8;
use strict;
use warnings qw/ all FATAL /;
use CGI qw/:standard/;
use JSON qw( decode_json );
use LWP;
use Template;
use Data::Dumper;
use constant JSON_URL => 'http://example.com/json';
### This code uses a literal JSON data item. The live code should
### fetch the real data from the JSON_URL. The code to do this
### is here but has been commented out
=comment
my $json = do {
my $ua = LWP::UserAgent->new;
my $resp = $ua->get(JSON_URL);
unless ( $resp->is_success ) {
die sprintf "Could not retrieve JSON data: %s", $resp->status_line;
}
$resp->decoded_content;
};
=cut
my $json = <<END;
{
"items": [
{
"sessionID": "session1",
"classField": "class1",
"startTimeField": "start1",
"leader": "leader1",
"professorField": "prof1",
"descriptionField": "desc1"
},
{
"sessionID": "session2",
"classField": "class2",
"startTimeField": "start2",
"leader": "leader2",
"professorField": "prof2",
"descriptionField": "desc2"
}
]
}
END
my $decoded_json = decode_json($json);
### Note that these variables are unused. This is just an example
### of a better way to extract a list of fields from an array
### Please remove this code before deploying
my $items = $decoded_json->{items};
my @sessionArr = map { $_->{sessionID} } @$items;
my @classArr = map { $_->{classField} } @$items;
my @timeArr = map { $_->{startTimeField} } @$items;
my @adminArr = map { $_->{leader} } @$items;
my @profArr = map { $_->{professorField} } @$items;
my @descArr = map { $_->{descriptionField} } @$items;
### All that needs to be done its to use CGI to print the HTTP
### header and invoke Template Toolkit to build an HTML page that
### includes the data from the JSON hash
print CGI->header(
-type => 'text/html',
-charset => 'utf-8',
);
my $tt = Template->new;
$tt->process(\*DATA, { json => $decoded_json } );
__DATA__
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<title>AU Study Sessions</title>
<link href="../css/bootstrap.css" rel="stylesheet">
<link href="../css/main.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->
<link rel="shortcut icon" href="../images/favicon.png">
<script src="../js/pace.js"></script>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300,600' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="preloader"></div>
<!-- ******************** MASTHEAD SECTION ******************* -->
<main id="top" class="masthead" role="main">
<div class="container">
<div class="logo">
<a href=""><img src="../images/aulogo2.png" alt="logo"></a>
</div>
<h1>View Study Sessions</h1>);
<table>
[% FOREACH item IN json.items %]
<tr>
<td>[% item.sessionID %]</td>
<td>[% item.classField %]</td>
<td>[% item.startTimeField %]</td>
<td>[% item.leader %]</td>
<td>[% item.professorField %]</td>
<td>[% item.descriptionField %]</td>
</tr>
[% END %]
<br/>
<br/>
<a href=""> Add a Study Session</a>
<!--
<a href="index.html#explore" class="scrollto">
<p>SCROLL DOWN TO EXPLORE</p>
<p class="scrollto--arrow"><img src="../images/scroll_down.png" alt="scroll down arrow"></p>
</a>
-->
</div> <!-- class="container" -->
</main>
<!-- ******************** FOOTER SECTION ******************** -->
<div class="container" id="explore">
<div class="section-title">
<h2>With Adelphi Study Sessions Website</h2>
<h4>You will be able to do the following</h4>
</div>
<section class="row features">
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="../images/service_01.png" alt="analytics-icon">
<div class="caption">
<h3>View Study Groups</h3>
<p>See the most up to date study sessions taking place on our garden city campus.</p>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="../images/service_02.png" alt="analytics-icon">
<div class="caption">
<h3>Add and create new study sessions</h3>
<p>If you or some classmates want to create a new study session you will be able to add a new group along with course information, sudy topics, and time and location taking place.</p>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="../images/service_03.png" alt="analytics-icon">
<div class="caption">
<h3>See description of session</h3>
<p>The student who creates the study session will give a short description about what the study session will cover.</p>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="../images/service_04.png" alt="analytics-icon">
<div class="caption">
<h3>Available on campus</h3>
<p>All study sessions will take place on our Garden City campus therefore are available to all students who commute or live on campus.</p>
</div>
</div>
</div>
</section>
<script src="../js/jquery.js"></script>
<script src="../js/bootstrap.js"></script>
<script src="../js/easing.js"></script>
<script src="../js/nicescroll.js"></script>
</div> <!-- class="container" id="explore" -->
</body>
</html>
输出
Content-Type: text/html; charset=utf-8
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<title>AU Study Sessions</title>
<link href="../css/bootstrap.css" rel="stylesheet">
<link href="../css/main.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->
<link rel="shortcut icon" href="../images/favicon.png">
<script src="../js/pace.js"></script>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300,600' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="preloader"></div>
<!-- ******************** MASTHEAD SECTION ******************* -->
<main id="top" class="masthead" role="main">
<div class="container">
<div class="logo">
<a href=""><img src="../images/aulogo2.png" alt="logo"></a>
</div>
<h1>View Study Sessions</h1>);
<table>
<tr>
<td>session1</td>
<td>class1</td>
<td>start1</td>
<td>leader1</td>
<td>prof1</td>
<td>desc1</td>
</tr>
<tr>
<td>session2</td>
<td>class2</td>
<td>start2</td>
<td>leader2</td>
<td>prof2</td>
<td>desc2</td>
</tr>
<br/>
<br/>
<a href=""> Add a Study Session</a>
<!--<a href="index.html#explore" class="scrollto">
<p>SCROLL DOWN TO EXPLORE</p>
<p class="scrollto--arrow"><img src="../images/scroll_down.png" alt="scroll down arrow"></p>
</a> -->
</div>
</main>
<!-- ******************** FOOTER SECTION ******************** -->
<div class="container" id="explore">
<div class="section-title">
<h2>With Adelphi Study Sessions Website</h2>
<h4>You will be able to do the following</h4>
</div>
<section class="row features">
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="../images/service_01.png" alt="analytics-icon">
<div class="caption">
<h3>View Study Groups</h3>
<p>See the most up to date study sessions taking place on our garden city campus.</p>
</div>
</div><! --/thumbnail -->
</div><! --/col-sm-6-->
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="../images/service_02.png" alt="analytics-icon">
<div class="caption">
<h3>Add and create new study sessions</h3>
<p>If you or some classmates want to create a new study session you will be able to add a new group along with course information, sudy topics, and time and location taking place.</p>
</div>
</div><! --/thumbnail -->
</div><! --/col-sm-6-->
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="../images/service_03.png" alt="analytics-icon">
<div class="caption">
<h3>See description of session</h3>
<p>The student who creates the study session will give a short description about what the study session will cover.</p>
</div>
</div><! --/thumbnail -->
</div><! --/col-sm-6-->
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="../images/service_04.png" alt="analytics-icon">
<div class="caption">
<h3>Available on campus</h3>
<p>All study sessions will take place on our Garden City campus therefore are available to all students who commute or live on campus.</p>
</div>
</div><! --/thumbnail -->
</div><! --/col-sm-6-->
</section><! --/section -->
<script src="../js/jquery.js"></script>
<script src="../js/bootstrap.js"></script>
<script src="../js/easing.js"></script>
<script src="../js/nicescroll.js"></script>
</body>
</html>