Skip to main content
removed "Drupal" from the title; removed tags from the title
Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Returning HTML from Drupal 8a controller

Source Link
Kevin Nowaczyk
  • 1.4k
  • 3
  • 13
  • 37

Returning HTML from Drupal 8 controller

I have followed the guide at http://befused.com/drupal/first-drupal8-module to create a simple Drupal 8 controller, and it works as described. Instead of printing "Hello World" I would like to create more complex HTML. I found a guide that appears to step through what I need to do at http://jonathanpatrick.me/blog/safe-markup. I added hook_theme to my flot_d8.module file:

function flot_d8_theme(array $existing, $type, $theme, $path) {
  return array(
    'flot_d8_my_template' => array(
      'template' => 'my-template',
      'variables' => array(
        'variable1' => NULL,
        'variable2' => NULL,
      ),
    ),
  );
}

and created the my-template.html.twig file exactly as shown:

<div class="my-template">
  This is the first variable: <b>{{ variable1 }}</b>.
  This is the second variable: <i>{{ variable2 }}</i>.
</div>

I'm guessing my problem is in the way I try to call the theme. My controller code is:

'flot_d8_my_template', '#variable1' => t("First"), '#variable2' => t("Second"), ]; } } Is this the correct way to print a `` to the screen? Do I need to register the template file in my info file somewhere?