How to specify a twig template for a custom Drupal 8 page? I'm trying to create a custom Drupal 8 route with predefined content, like /about-us, which is not a node, just a standalone page.

The following tutorial is good only up to the point where I need to add the page content using #markup: https://www.drupal.org/node/2116767

How can I specify a twig template that I would like to use for page content?
I got that to work. Here's my solution.

Create a template in path/to/your/module/templates/my-custom-page.html.twig.

/**
 * Implements hook_theme().
 */
function mymodule_theme($existing, $type, $theme, $path)
{
    return [
        'my_custom_page' =&gt; [
            'render element' =&gt; 'custom_page',
            'path' =&gt; $path . '/templates',
            'template' =&gt; 'my-custom-page',
            'variables' =&gt; [
                'var1' =&gt; 'default value for var1',
            ],
        ],
    ];
}


I couldn't figure out what render element is for, but it is required.

variables element defines the variables available in your template and provides default values for them. You can override them in your controller.

use Drupal\Core\Controller\ControllerBase;

/**
 * Class CustomPageController
 */
class CustomPageController extends ControllerBase
{

    public function getPage()
    {
        return array(
            '#theme' =&gt; 'my_custom_page',
            '#var1' =&gt; 'Actual value for var1',
        );
    }

}


Clear the cache and visit your custom page route. Everything should work.

Helpful links:


Render API
overview
hook_theme()