I'm trying to output an array in twig, but I don't have enough experience in Twig.
I just don’t know how to output a non-associative array if the keys are index Array $c_comments of such
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 %}
This is how I display an array in php, also needed in twig
<div class="c_comments"><i>'.$c_comments[$i][0].' '.$c_comments[$i][1].' Wrote:</i><br>'.$c_comments[$i][2].'</div>';
Because of this, the template simply doesn't work. So transfer the data to the template
$item = ['first' => $number_f, 'two' => $number_t, 'comment' => $c_comments, 'c_page' => $c_page,'pagination' => $varPagination];
$this->registry['template']->set('item', $item);
Where is my mistake?
If your variable is c_comments
, you can use this for
loop :
// 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 %}