4

How do you theme stuff in Drupal 8? In 7, for example, if you had some data items you could call item_list, via the theme function, pass in a render array and get your markup back.

This is a great resource for those interested: Render API, but no examples that I can see.

How do I do this in Drupal 8? For example, how do I theme an image? Sure, I can probably construct my render array, but does it then have to go off to a twig template or something. Are there any examples yet?

Thanks.

8

It seems to be similar to D7. Just use a render array:

$list = array('apples', 'peaches', 'pears');
$render_array = array(
  '#theme' => 'item_list',
  '#items' => $list,
  '#title' => t('Argument Information'),
);
return $render_array;

I searched '#theme' in core\modules to find examples. Not sure if this is the 'correct' way though?

  • 2
    Yes, it works exactly like in D7 if you use render arrays. Whenever possible, use render arrays, if you really can't (e.g. in hook_help() or other places that can't return a render array), you can use \Drupal::theme()->render() as a replacement for theme(). – Berdir Jan 30 '15 at 11:49

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.