Data returned from the controller is not showing in the template file 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' =&gt; [
      'variables' =&gt; [
        'var' =&gt; [],
        'title' =&gt; ''
      ]
    ]
  ];
}


module.routing.yml

module.event:
  path: '/path/{dynamic}'
  defaults:
    _controller: '\Drupal\module\controller::RunQuery'
    _title: 'Module'
  requirements:
    _permission: 'access content'


module-template.html.twig

&lt;p&gt; test {{ title }}&lt;/p&gt;
&lt;ul&gt;
{% for user in var %}
  &lt;li&gt; {{ user.title }} &lt;/li&gt;
{% endfor %}
&lt;/ul&gt;
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' =&gt; 'module_template',
      '#var'   =&gt; $ok,
      '#title' =&gt; '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 }}.
I don't know what exactly you are trying to do as var never seems to be passed an array or object but I'll give you a working example. Your controller should extend ControllerBase and use public function content() to output stuff. Also pay attention to reference the correct namespace and pay attention to upper and lower case writing as well.

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' =&gt; [
      'variables' =&gt; [
        'var'   =&gt; NULL,
        'title' =&gt; NULL,
      ],
    ],
  ];
}


module.routing.yml

module.event:
  path: '/path/{user}'
  defaults:
    _controller: '\Drupal\module\Controller\Controller::content'
    _title: 'Module'
  requirements:
    _permission: 'access content'


templates/module-template.html.twig

&lt;p&gt; test {{ title }}&lt;/p&gt;
&lt;div&gt;{{ var.getUsername }}&lt;/div&gt;
test 2


src/Controller/Controller.php

namespace Drupal\module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountInterface;

class Controller extends ControllerBase {

  public function content(AccountInterface $user) {

    return [
      '#theme' =&gt; 'module_template',
      '#var'   =&gt; $user,
      '#title' =&gt; 'Title',
    ];
  }

}




Now you can access /path/1 or whatever user ID you want to get this user's name printed.

With the Devel sub-module Kint installed you can use {{ kint() }} or {{ kint(var) }} inside the template to check out what else you can get and print.