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' => [
            'render element' => 'custom_page',
            'path' => $path . '/templates',
            'template' => 'my-custom-page',
            'variables' => [
                'var1' => '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' => 'my_custom_page',
            '#var1' => 'Actual value for var1',


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

Helpful links:

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.