Passing variables to custom templates In Drupal 7 I have a web site with dozens of custom templates. I used theme() to pass variables to my templates defined in hook_theme() in a page callback defined in hook_menu. How do you do this in Drupal 8? The theme() function is no longer available.
Drupal 8 uses render arrays for the structure, and drupal_render() to render them (as can Drupal 7 for themeing, incidentally). 

The examples from the change record are as good as any:

Drupal 7

// Theme a table with header and row data.
$markup = theme('table', array('header' =&gt; $header, 'rows' =&gt; $rows, 'attributes' =&gt; array('id' =&gt; 'my-module-table')));

// Render a pager.
$markup = theme('pager');


Drupal 8

// Theme is available as an element type (may have additional processing in rendering).
$table = array(
  '#type' =&gt; 'table',
  '#header' =&gt; $header,
  '#rows' =&gt; $rows,
  '#attributes' =&gt; array(
    'id' =&gt; 'my-module-table',
  ),
);
$markup = drupal_render($table);

// Pager is not an element type, use #theme directly.
$pager = array('#theme' =&gt; 'pager');
$markup .= drupal_render($pager);


There's a useful warning too:


  Also note that when preparing output for Twig templates (in preprocess functions) it is preferable to avoid calling drupal_render() whenever possible. The Twig environment in Drupal 8 will automatically render any render arrays printed in the template, allowing for manipulation by later preprocess functions and preventing unnecessary rendering if the variable is never printed in the template.