Display custom variables We have a twig template, node--page.html.twig where we want to display the custom variable ns-custom-template. The variable is created from a custom module, mymodule with the following code below. 

How can I create a variable, populate it using the ns-custom-template variable, and display it in the node--page.html.twig template?

Here is what I tried, but when using kint(test_var) I get NULL returned.

mymodule.module

function mymodule_theme($existing, $type, $theme, $path) {
  return [
    'ns_custom_template' =&gt; [
      'variables' =&gt; [
        'test_var' =&gt; NULL,
      ],
    ],
  ];
}


Inside mymodule's template folder is ns-custom-template.html.twig

&lt;p&gt; Testing my variable {{ test_var }} &lt;/p&gt;


MyModuleController class

class MyModuleController extends ControllerBase {
  public function content() {
    $config = $this-&gt;config('mymodule.settings');
    $config_value = $config-&gt;get('my_config_value');
    return [
      '#theme' =&gt; 'ns_custom_template',
      '#test_var' =&gt; $config_value,
    ]
  }
}


mymodule's routing file:

mymodule.module_controller_content:
  path: '/mymodule/content'
  defaults:
    _controller: '\Drupal\mymodule\Controller\MyModuleController:content'
    _title: 'content'
  requirements:
    _permission: 'access content'


MyModuleForm

class MyModuleForm extends ConfigFormBase {
  protected function getEditableConfigName() {
    return [
      'mymodule.setting',
    ],
  }

  public function getFormId() {
    return 'mymodule_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this-&gt;config('mymodule.setting');
    $form['my_config_value'] = [
      '#type' =&gt; 'textarea',
      '#title' =&gt; $this-&gt;t('My config value'),
      '#description' =&gt; $this-&gt;t('Enter some value'),
      '#default_value' =&gt; $config-&gt;get('my_config_value'),
    ];
    return parent::buildForm($form, $form_state);
  }

  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);

    $this-&gt;config('mymodule.setting')
      -&gt;set('my_config_value', $form_state-&gt;getValue('my_config_value'))
      -&gt;save();
  }
}


node--page.html.twig

&lt;article&gt;
  {{ ns_custom_template }} {# returns null #}
  {{ kint(ns_custom_template }} {# returns null #}
&lt;/article&gt;


For reference, I am trying to emulate the steps on Define a custom template for module output page.
The variable you are looking for is not ns_custom_template but test_var

To get all variables in your twig file, you can try {{ kint() }}

To get the variable you have previously declared in mymodule_theme, you can try {{ kint(test_var) }}

BUT there is another mistake:

The twig file in which you will find test_var is not node--page.html.twig but only ns-custom-template.html.twig

if you want to have test_var inside node--page.html.twig, you can use a preprocess function : function template_preprocess_node with a code like:

$var['test_var']='my value';


EDIT:It seems that you would like to use the template ns_custom_template in a node template: to achieve this, you need to populate your variable test_var with a render array having the name defined in mymodule_theme as a value of the #theme key.

function mymodule_preprocess_node(&amp;$var) {
  $var['my_free_var_name'] = ['#theme' =&gt; 'ns_custom_template', '#test_var' =&gt; 'your value'];
}


After doing this, you can display your template inside node--page.html.twig bu using the following code:

{{ my_free_var_name }}