Create form via a controller I have a template in which I've manually built a form. It renders some variables that come from a controller.

I've looked over the Form API and I think I should build the form using that. But I'm not quite sure how to go about it.

I'm relatively new to Drupal 8 and to be perfectly honest the Documentation is good, if one knows what to look for. It's not the case here.

Basically what I'm asking, is it possible(or rather advised) to somehow grab the form in the controller, and instead of passing the variables to the template, simply pass the created form to the template?

I'm assuming it would be better to do that, although I'm note quite sure that's the case.
This is definitely possible (and preferable). There are a few ways to do this, but I think the preferred approach is to first specify your form in your routing entry (mymodule.routing.yml)

mymodule.some.route:
  path: '/foo/bar'
  defaults:
    _form: '\Drupal\mymodule\Form\MyForm'
    _title: 'My form title'


The routing system will automatically build your form for you. The class \Drupal\mymodule\Form\MyForm should implement FormInterface. An easy starting point is to extend \Drupal\Core\Form\FormBase.

There are several examples of this in core. For instance, the entity.user.admin and user.admin_permissions routes in users.routing.yml.