How can I render an entity in a string? Is it possible? To use the viewBuilder of an entity to generate the complete html and save that output into a string instead of sending it to a browser.

It would open the possibility to have email messages render using twig templates.

I have been poking around the different services and using the renderer service I can render the page-specific output as html but I can't find a way to get the complete HTML code into a string.

Matt.
Your question is "render a drupal 8 entity in a string". This is an example how to do this. Get an entity. Get a view builder for the entity node and us it to build your render array with the display mode "full". Render this with renderRoot to the string $html.

$node = \Drupal::entityManager()-&gt;getStorage('node')-&gt;load($nodeid);
$view_builder = \Drupal::entityManager()-&gt;getViewBuilder('node');
$renderarray = $view_builder-&gt;view($node, 'full');
$html = \Drupal::service('renderer')-&gt;renderPlain($renderarray);


You can do this for any entity. By default this will use the node.html.twig as configured in your theme. But you can change your render array before rendering.

Edit:
For emails use renderPlain().