[英]symfony3 twig and show session array
我在會話中有數組,但無法顯示。 我的代碼:控制器:
$game = $request->query->get('game');
$type = $request->query->get('type');
$odd = $request->query->get('odd');
$kupon = array(
'game' => $game,
'type' => $type,
'odd' => $odd,
);
$this->get('session')->set('kupon', $kupon);
例如,“游戲”為:“阿森納-切爾西”,“類型”為數字,如1,奇數為浮點數,如“ 2.2”。
樹枝文件:
{% if app.session.get('kupon') is not null %}
<table>
{% for kupon in session %}
<tr>
<td>{{ kupon.game }}</td>
<td>{{ kupon.type }}</td>
<td> </td>
</tr>
{% endfor %}
</table>
一切正常,但是當我嘗試登錄並顯示數據會話時,出現此錯誤:
在第13行的baw \\ kupon.html.twig中,無法訪問字符串變量(“ PNdjNUeuZ_d5uJlm1VG7zPZhp2Vb4CY3nDf93vAQ574”)上的屬性(“游戲”)。
此變量來自登錄后的會話,我檢查了有關它的轉儲信息:
array(3) {
["_csrf/login"]=> string(43) "PNdjNUeuZ_d5uJlm1VG7zPZhp2Vb4CY3nDf93vAQ574" ["login"]=> string(4) "test"
["kupon"]=> array(3) { ["game"]=> string(31) "Arsenal Londyn - Chelsea Londyn" ["type"]=> string(1) "1" ["odd"]=> string(3) "2.2" } }
現在我還不知道要解決此問題。
{% for kupon in session %}
將不會遍歷app.session.get('kupon')
您想要做的是:
{% for kupon in app.session.get('kupon') %}
但是查看您轉儲的數據, app.session.get('kupon')
僅僅是一個數據集,因此您甚至無法循環播放(具有所需的結果)...
只是:
{{ app.session.get('kupon').game }}
有關數據的其他說明:您的會話中有以下內容:
"kupon" => [
"game" => ...
"type" => ...
...
]
為了能夠遍歷這些,您需要收集數據類型:
"kupon" => [
[
"game" => ...
"type" => ...
...
],
[
"game" => ...
"type" => ...
...
],
....
]
試試下面
{% if app.session.get('kupon') is not null %}
{% set kupon = app.session.get('kupon') %}
<table>
<tr>
<td>{{ kupon.game }}</td>
<td>{{ kupon.type }}</td>
<td> </td>
</tr>
</table>
{% endif %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.