Drupal 8 Passing Custom Variables to Theme Template
<p>So I have a controller with a route already configured my action looks like this</p>

<pre><code>/**
 * List of brands
 *
 * @return array
 */
public function listAction()
{
    $brandIds = \Drupal::entityQuery('node')
        -&gt;condition('type', 'brand')
        -&gt;sort('title', 'asc')
        -&gt;execute();

    return [
        'addition_arguments' =&gt; [
            '#theme' =&gt; 'page--brands',
            '#brands' =&gt; is_array($brandIds) ? Node::loadMultiple($brandIds) : [],
            '#brands_filter' =&gt; \Drupal::config('field.storage.node.field_brand_categories')-&gt;get()
        ]
    ];
}
</code></pre>

<p>I would like to use #brands and #brands_filter in my twig template theme file page--brands, but I never see it go through.</p>

<p>Can anyone help?</p>

<p>Thank you</p>

<p><strong>UPDATE</strong></p>

<p>Worked it out</p>

<p>In you modules my_module.module file add the following</p>

<pre><code>function module_theme($existing, $type, $theme, $path)
{
    return [
        'module.brands' =&gt; [
            'template' =&gt; 'module/brands',
            'variables' =&gt; ['brands' =&gt; []],
        ],
    ];
}
</code></pre>

<p>In your controller use</p>

<pre><code>return [
        '#theme' =&gt; 'mymodule.bands',
        '#brands' =&gt;is_array($brandIds) ? Node::loadMultiple($brandIds) : []
]
</code></pre>

<p>This will inject the variable Hope this helps omeone else who has this problem, wish the docs were better :)</p>