在这里以下舞者教程:

http://search.cpan.org/dist/dancer/lib/dancer/tutorial.pod

我正在使用此模式使用我自己的sqlite3数据库

CREATE TABLE if not exists location (location_code TEXT PRIMARY KEY, name TEXT, stations INTEGER);
CREATE TABLE if not exists session (id INTEGER PRIMARY KEY, date TEXT, sessions INTEGER, location_code TEXT, FOREIGN KEY(location_code) REFERENCES location(location_code));

我的舞者代码(Helloworld.pm)用于数据库:

package helloWorld;
use Dancer;
use DBI;
use File::Spec;
use File::Slurp;
use Template;

our $VERSION = '0.1';

set 'template' => 'template_toolkit';
set 'logger'   => 'console';

my $base_dir = qq(/home/automation/scripts/Area51/perl/dancer);

# database crap
sub connect_db {
 my $db = qw(/home/automation/scripts/Area51/perl/dancer/sessions.sqlite);
 my $dbh = DBI->connect("dbi:SQLite:dbname=$db", "", "",
   { RaiseError => 1, AutoCommit => 1 });
 return $dbh;
}

    sub init_db {
 my $db = connect_db();
 my $file = qq($base_dir/schema.sql);
 my $schema = read_file($file);
 $db->do($schema) or die $db->errstr;
    }

get '/' => sub {
 my $branch_code = qq(BPT);
 my $dbh = connect_db();
 my $sql = q(SELECT * FROM session);
 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
 $sth->execute or die $dbh->errstr;
 my $key_field = q(id);
 template 'show_entries.tt', {
  'branch' => $branch_code,
  'data' => $sth->fetchall_hashref($key_field),
 };
};

init_db();
true;

在网站上尝试了示例模板,不起作用。

<% FOREACH id IN data.keys.nsort %>
  <li>Date is: <% data.$id.sessions %> </li>
<% END %>

生产页面,但没有数据。我该如何对此进行故障排除,因为控制台/CLI中没有线索?

* 更新 *如果我将数据库代码更改为:

get '/' => sub {
    my $branch_code = qq(BPT);
    my $dbh = connect_db();
    my $sql = 'SELECT * FROM session';
    #my $sth = $dbh->prepare($sql) or die $dbh->errstr;
    #$sth->execute or die $dbh->errstr;
    #my $key_field = q(id);
    my $entries = $dbh->selectall_arrayref($sql,{});
    template 'show_entries.tt', {


'branch' => $branch_code,
        #'data' => $sth->fetchall_hashref('id'),
        'data' => @$entries,
    };
};

我从模板中的表中得到一个结果。因此,该信息正在传递,但是模板的语法无法正常工作。这确实符合模板工具包语法。

谢谢

布诺夫

编辑/分辨率**

David让我想起了Data :: Dumper,这证实了问题的确是模板配置。我在配置文件中评论了模板指令,认为它是多余的,因为它在代码本身中。错误的!!!它必须在YAML中配置。删除配置中的octothorpe将所有内容设置为权利。现在,我只是为了不尝试Data :: Dumper而感到尴尬。谢谢大卫!

有帮助吗?

解决方案

首先,请确保您将自己认为要传递给模板的内容。

将$ sth--> fetchall_hashref($ key_field)的结果分配给临时标量,然后用data :: dost :: deta data :: Dumper将其转储(或参见 舞者::插件:: debugdump 为了使其变得容易死亡)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top