Perl dancer:将数据库信息传递给模板

发布于 2024-10-12 10:29:00 字数 2710 浏览 1 评论 0原文

此处遵循 Dancer 教程:

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));

我的数据库的 dancer 代码 ( 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,
    };
};

我从模板中的表中得到一个结果。所以信息正在传递,但是 模板的语法不按描述工作。这确实符合 Template Toolkit 语法。

谢谢

Bubnoff

编辑/解决方案 **

David 让我想起了 Data::Dumper,它确认问题确实出在模板配置上。我注释掉了配置文件中的模板指令,认为它是多余的,因为它位于代码本身中。错误的!!!必须在 YAML 中配置。在配置中删除 octothorpe 会将所有内容设置为权限。现在我只是因为没有首先尝试 Data::Dumper 而感到尴尬。谢谢大卫!

Following Dancer tutorial here:

http://search.cpan.org/dist/Dancer/lib/Dancer/Tutorial.pod

I'm using my own sqlite3 database with this schema

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));

My dancer code ( helloWorld.pm ) for the database:

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;

Tried the example template on the site, doesn't work.

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

Produces page but with no data. How do I troubleshoot this as no clues
come up in the console/cli?

* UPDATE *
If I change the database code to this:

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,
    };
};

I get one result from the table in the template. So the info is being passed, but the
syntax for the template does not work as described. This does fit with Template Toolkit syntax.

Thanks

Bubnoff

EDIT/RESOLUTION **

David reminded me of Data::Dumper which confirmed that the problem was indeed with the Template configuration. I commented out the template directive in the config file thinking that it would be redundant since it was in the code itself. WRONG!!! It must be configured in the YAML. Deleting an octothorpe in the config set everything to rights. Now I'm just embarrassed about not trying Data::Dumper in the first place. Thanks David!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2024-10-19 10:29:00

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

将 $sth->fetchall_hashref($key_field) 的结果分配给临时标量,然后使用 Data::Dump 或 Data::Dumper 转储它(或参见 Dancer::Plugin::DebugDump 是一种让它变得非常简单的方法)。

First of all, make sure you're passing what you think you're passing to the template.

Assign the result of $sth->fetchall_hashref($key_field) to a temporary scalar, then dump it with Data::Dump or Data::Dumper (or see Dancer::Plugin::DebugDump for a way to make it dead easy).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文