简体   繁体   English

perl舞者:将数据库信息传递给模板

[英]perl dancer: passing database info to template

Following Dancer tutorial here: 在这里跟随舞者教程:

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

I'm using my own sqlite3 database with this schema 我在这种模式下使用自己的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));

My dancer code ( helloWorld.pm ) for the database: 我的数据库舞者代码(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;

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? 我如何解决此问题,因为控制台/ 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. 这确实符合Template Toolkit语法。

Thanks 谢谢

Bubnoff Bubnoff

EDIT/RESOLUTION ** 编辑/分辨率**

David reminded me of Data::Dumper which confirmed that the problem was indeed with the Template configuration. David使我想起了Data :: Dumper,它确认问题确实出在模板配置上。 I commented out the template directive in the config file thinking that it would be redundant since it was in the code itself. 我注释掉了config文件中的template指令,以为它是多余的,因为它位于代码本身中。 WRONG!!! 错误!!! It must be configured in the YAML. 必须在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. 现在,我为不首先尝试使用Data :: Dumper而感到尴尬。 Thanks David! 谢谢大卫!

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). 将$ sth-> fetchall_hashref($ key_field)的结果分配给一个临时标量,然后使用Data :: Dump或Data :: Dumper转储它(或参见Dancer :: Plugin :: DebugDump以使它变得非常简单) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM