2017-10-11 40 views
0

我有一个带有计算机表的SQLite数据库。我在电脑桌上有两排。请求的结果不显示在模板工具包中

我想要得到所有电脑,并在模板工具包模板中显示结果。

这是 Dancer2 控制器代码,它使用 Dancer2::Plugin::Auth::TinyDancer2::Plugin::DBIC

get '/listallmachine' => needs login => sub { 
    my $computerRs = schema('default')->resultset('Computer'); 
    my @computers = $computerRs->all; 
    template 'listmachine' => { 
     'title'  => 'Liste des machines', 
     'msg'  => get_flash(), 
     'computers' => \@computers 
    }; 
}; 

而对于模板:

[% FOREACH c IN computers %] 
    <tr> 
     <td>[% c.ip %]</td> 
     <td>[% c.uuid %]</td> 
    </tr> 
[% END %] 

配置文件:

# configuration file for development environment 

# the logger engine to use 
# console: log messages to STDOUT (your console where you started the 
#   application server) 
# file: log message to a file in log/ 
logger: "console" 

# the log level for this environment 
# core is the lowest, it shows Dancer2's core log messages as well as yours 
# (debug, info, warning and error) 
log: "core" 

# should Dancer2 consider warnings as critical errors? 
warnings: 1 

# should Dancer2 show a stacktrace when an 5xx error is caught? 
# if set to yes, public/500.html will be ignored and either 
# views/500.tt, 'error_template' template, or a default error template will be used. 
show_errors: 1 

# print the banner 
startup_info: 1 

plugins: 
     DBIC: 
     default: 
      dsn: dbi:SQLite:dbname=papt.db 

该模板不显示任何内容。你有什么想法吗?

+1

您是否检查过“@ computers”中的内容? – Borodin

+0

是的,这是我的电脑数据:请求的结果。如果我不使用模板,它的工作... – Oneill

+0

好的。我认为我的答案(如下)会有所帮助,但似乎还有更多的事情在这里进行。如果您消除了模板工具包并在Perl代码中循环访问数组,那么会发生什么?如果在模板中包含'[%computers.size%]',你会得到什么?或'[%computers.0%]'? –

回答

2

您需要参考@computers

get '/listallmachine' => needs login => sub { 
    my $computerRs = schema('default')->resultset('User'); 
    my @computers=$computerRs->all; 
    template 'listmachine' => { 
     'title'  => 'Liste des machines', 
     'msg'  => get_flash(), 
     'computers' => \@computers, # Note: Take reference here. 
    }; 
}; 

更新:好吧,我想我现在可以解释这一点。

在评论中,你说get_flash()返回“一个Hashmap”(我认为,这意味着“哈希”)。假设它返回包含两个键/值对的散列(one => 1two => 2)。这意味着,您发送到template哈希看起来就像这样:

{ 
    title  => 'Liste des machines', 
    msg  => one => 1, two => 2, 
    computers => \@computers 
}; 

但是,这一切都只是一个平坦的列表。 Perl会这样解释:

{ 
    title  => 'Liste des machines', 
    msg   => 'one', 
    1   => 'two', 
    2   => 'computers', 
    \@computers => undef, 
}; 

你看到发生了什么?由于从get_flash()返回的多个值,您的键/值对已全部脱节。你不再有一个叫做computers的散列键。这就是为什么模板找不到名为computers的变量 - 它不再存在。

修复的方法是取一个参考到从get_flash()返回哈希:

{ 
    title  => 'Liste des machines', 
    msg  => { get_flash() }, 
    computers => \@computers 
}; 

参考防止被压扁成一个列表的哈希。你的数据结构是这样的:

{ 
    title  => 'Liste des machines', 
    msg  => { one => 1, two => 2 }, 
    computers => \@computers 
}; 

(。迂腐,问题是,子程序不返回哈希 - 他们返回列表列表只变成一个哈希当你把它存储在一个散列变量)

+0

感谢您的回答。我参考测试这个解决方案,但不工作...你认为我的模板foreach是正确的吗? – Oneill

0

好吧,我明白这个问题。这是我的get_flash()函数,当我移除“msg”元素时,显示正常。所以我忘记了我的参考,并且“get_flash”功能很糟糕。感谢您的帮助。

+0

对'get_flash()'的调用是否可能返回多个值? –

+0

get_flash()的返回值是一个Hashmap。我认为我没有正确使用get_flash的返回。但目前这是行之有效的:)谢谢你的帮助。 – Oneill

+0

如果'get_flash()'返回一个散列值,那么在放入模板散列值之前,需要对其进行引用。 'msg => {get_flash()}'。 –

相关问题