How to render a form in custom template? I'm looking to add a form inside a custom template. The form, if I open the path registered inside the file cmodule.routing.yml, works but if i try to use it inside a custom template it doesn't work: with a dump(form) or dump(form.anyfield) i get NULL.

cmodule.routing.yml:

cmodule.manageform:
  path: '/cmodule/manageform'
  defaults:
    _title: 'Documents'
    _form: '\Drupal\cmodule\Forms\RequestForm'
  requirements:
    _permission: 'access content'
cmodule.manage:
  path: '/cmodule/manage'
  defaults:
    _controller: '\Drupal\cmodule\Controller\CmoduleController::manageAction'
    _title: 'Custom Documents'
  requirements:
    _permission: 'access content'


RequestForm.php:

/**
 * {@inheritdoc}
 *
 */
public function buildForm(array $form, FormStateInterface $form_state) {

    $form['type'] = array(
        '#type' =&gt; 'radios',
        '#options' =&gt; array(
            '0' =&gt;t('DOC'),
            '1' =&gt;t('PDF')
        ),
    );
    $form['pid'] = array(
        '#type' =&gt; 'textfield',
        '#title' =&gt; t('ID:'),
        '#required' =&gt; FALSE
    );
    $form['submit'] = array(
        '#type' =&gt; 'submit',
        '#value' =&gt; $this-&gt;t('Submit'),
        '#attributes'=&gt; ['class'=&gt;['glyphicon', 'glyphicon-search']],
    );


    return $form;
}


cmodule.module:

/**
 * Implements hook_theme().
 */
function cmodule_theme(){

    $templates = array(
        'manage_cmodule_page' =&gt; array(
            'variables' =&gt;
                [
                    'id' =&gt; NULL,
                    'form' =&gt; NULL
                ],
            'template' =&gt;'manage_cmodule',
            'render element' =&gt; 'form'
        )
    );

    return $templates;
}


the controller contains:

public function manageAction() {

        $id = 1;

        $form = \Drupal::formBuilder()-&gt;getForm('Drupal\cmodule\Forms\RequestForm');

        $form['type']['#title_display'] = 'invisible';

        return [ '#theme' =&gt; 'manage_cmodule_page',
            '#form' =&gt; $form,
            '#id' =&gt; $id,
        ];
    }


and then the template/view (contains):

&lt;div&gt;
 {{ form.form_token }}
{{ form.form_build_id }}
{{ form.form_id }} 
{{form.type}}
{{form.submit}}
&lt;/div&gt;
etc..


But as I said above the template doesn't show the form, but show (if i print it) the #id value passed through the controller. 
Do you have any suggestions?!



UPDATE #1
I tried also to follow the suggestion of Beebee, and then this and this so the code is:

controller:

controller contains:

public function manageAction() {

        $form = \Drupal::formBuilder()-&gt;getForm('Drupal\cmodule\Forms\RequestForm');

        $render['#form'] = $form;
        $render['theme'] = 'manage_cmodule_page';
        return $render;;
    }


and the implementation of hook_theme in .module file is:

return [
    'manage_cmodule_page' =&gt; [
        'template' =&gt;'manage_cmodule',
        'render element' =&gt; 'form',
    ]
];


The result is an empty page with an error inside the apache log:


  [php7:error] [pid 12660:tid 1592] [client ::1:59521] PHP Fatal error: 
  Out of memory (allocated 270532608) (tried to allocate 266354688
  bytes) in
  C:\drupal\vendor\twig\twig\lib\Twig\Extension\Debug.php on
  line 60, referer: http://localhost/drupal/


if I remove the following row from the controller

$render['theme'] = 'manage_atps_page';


the page loads, but it is empty (without any form).

UPDATE #2

I fixed my issue following also the idea of Alex Kuzava. Thus I used the code of UPDATE 1, with the new directory of the form, and then I added these 3 lines inside the  part of my template (otherwise the submit button doesn't work).

{{ form.form.form_build_id }} 
{{ form.form.form_id }}
{{ form.form.form_token }}
'render element' => 'form'


Your render element is form which means all provided variables will be passed there.

In your twig template you can render your form like here

&lt;div&gt;{{ form.form }}&lt;/div&gt;




UPDATE #1

Wow man, you have wrong namespace Drupal\cmodule\Forms\RequestForm

Should be Drupal\cmodule\Form\RequestForm

Please rename Forms to Form (the same for directory) and clear cache. It should help you.