Drupal 8's HTML elements render and themes | Drupal 8

Drupal 8's HTML elements render and themes

Submitted by editor on Fri, 09/16/2016 - 10:22
Question

Examples of drupal 8 html elements render and themes

Use custom twig template or alter an existence.

Create a custom module then use hook_theme()

HTML Elements

Example : Create a Table

$header = ['#','Name', 'Mail'];
$data = [
  [1,'Name 1', 'Mail1@example.com'],
  [2,'Name N°2', 'second@example.com'],
];

$output[] = array(
  '#theme' => 'table',
  //'#cache' => ['disabled' => TRUE],
  '#caption' => 'The table caption / Title',
  '#header' => $header,
  '#rows' => $data,
);

Example : Create a List

$output = [
  '#theme' => 'item_list',
  '#list_type' => 'ul',
  '#title' => 'My List',
  '#items' => ['Apple', 'Car'],
  '#attributes' => ['class' => 'myclass'],
  '#wrapper_attributes' => ['class' => 'my_list_container'],
];

Example: Create a link

$output = [
  '#title' => $this->t('Examples'),
  '#type' => 'link',
  '#url' => Url::fromRoute('examples.description')
];

Example :

 

Add new comment