Custom twig template is just this:
<ul id="rows">
{% for product in products %}
<li>{{ product.title }} <a href="{{ product.image }}">Image</a> {{ product.description }}</li>
{% endfor %}
</ul>
My custom module sends an array of products, which are something like this
array(1) { [0]=> array(3) { ["title"]=> string(9) "Product 3" ["image"]=> string(56) "/sites/default/files/2019-11/128px-Small_Storage_Box.png" ["description"]=> string(22) "Description 3" } }
And I am sending it from a controller like this:
return array(
'#title' => t('Return products list'),
'#products' => $products,
'#theme' => 'return-products',
);
In my .module file I've defined hook as:
<?php
function return_products_theme($existing, $type, $theme, $path) {
return array(
'products' => [
'variables' => [],
]);
}
I know data is there in an array, it just doesn't go inside twig template. Even if I type some
Test it won't be shown.
'variables' => ['products' => []],
.'variables' => ['products' => []],
doesn't help. Still thanks.{{ product['title'] }}
'#theme' => 'products'
since that's the current name of your template file inhook_theme()
. But the answer below is probably what you were trying to do.