I am sending data from a custom module controller to a template file using the following code.
module.info.yml
name: Module
description: Module description
package: Custom
type: module
version: 1.0
core: 8.x
module.module
function module_theme($existing, $type, $theme, $path) {
return [
'module_template' => [
'variables' => [
'var' => [],
'title' => ''
]
]
];
}
module.routing.yml
module.event:
path: '/path/{dynamic}'
defaults:
_controller: '\Drupal\module\controller::RunQuery'
_title: 'Module'
requirements:
_permission: 'access content'
module-template.html.twig
<p> test {{ title }}</p>
<ul>
{% for user in var %}
<li> {{ user.title }} </li>
{% endfor %}
</ul>
test 2
controller.php
namespace Drupal\module;
class controller {
public function RunQuery($dynamic) {
// Some query here which work fine and return what I want
$ok = 'Some value';
return [
'#theme' => 'module_template',
'#var' => $ok,
'#title' => 'Title',
];
}
}
This will return only test and test 2 in the template file, so the template file is called by the module controller, but it does not show {{ title }}
and {{ user.title }}
.