Nov 14, 2019 in Drupal 8, Snippets

You create a custom controller and want to show a table that resembles the core back-end tables. This snippet shows you how to do it.

use Drupal\Core\Render\Markup;

$rows = [
  [Markup::create('<strong>test 1</strong>'),'test'],
  [Markup::create('<s>test 2</s>'), 'test'],
  [Markup::create('<div>test 3</div>'), 'test'],
];
$header = [
  'title' => t('Title'),
  'content' => t('Content'),
];
$build['table'] = [
  '#type' => 'table',
  '#header' => $header,
  '#rows' => $rows,
  '#empty' => t('No content has been found.'),
];
return [
  '#type' => '#markup',
  '#markup' => render($build)
];

The result looks like this:

Render a table with html in drupal 8