[英]Php output array in Twig
我正在嘗試在樹枝中輸出一個數組,但我沒有足夠的經驗在Twig。
如果鍵是索引Array $ c_comments,我只是不知道如何輸出非關聯數組
Array
(
[0] => Array
(
[0] => 20:28 28:05:2019
[1] => dokl
[2] => 45645
)
[1] => Array
(
[0] => 20:27 28:05:2019
[1] => Alex
[2] => 546
)
)
{% for vars.item.c_page in vars.item.comment %}
<li>{{ vars.item.comment. }}</li>
{% endfor %}
這就是我在php中顯示數組的方式,也是在twig中需要的
<div class="c_comments"><i>'.$c_comments[$i][0].' '.$c_comments[$i][1].' Wrote:</i><br>'.$c_comments[$i][2].'</div>';
因此,模板根本不起作用。 因此,將數據傳輸到模板
$item = ['first' => $number_f, 'two' => $number_t, 'comment' => $c_comments, 'c_page' => $c_page,'pagination' => $varPagination];
$this->registry['template']->set('item', $item);
我的錯誤在哪里?
如果您的變量是c_comments
,則可以使用此for
循環:
// PHP
$data = [
[
'20:28 28:05:2019',
'dokl',
45645,
],
[
'20:27 28:05:2019',
'Alex',
546,
]
];
echo $twig->render('index.html', ['c_comments' => $data]);
<!-- index.html -->
{% for key, comment in c_comments %}
<i>{{ comment[0] }} {{ comment[1] }} Wrote: {{ comment[2] }}</i><br>
{% endfor %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.