2017-10-11 42 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()}'。 –

相關問題