Let's assume you have a "product" node
$node, and for some reason you want to render HTML from $node into a variable $html using a Twig template. I'm a bit desperate yet, what would you do?
Additional info 1: We are not in a controller, but in a simple class that is registered as a service.
Additional info 2: 4k4 's answer does work if I call my class from a controller. But as soon I call it from a Drupal Console command I get an Exception "The theme implementations may not be rendered until all modules are loaded."
Edit: The complete solution
Obviously not all modules are loaded if we call the service inside a Drupal Console command. The Theme Manager will honor this with the aforementioned exception, so we have to force load the modules:
\Drupal::moduleHandler()->loadAll(); // Load all modules if not loaded already
Now it's safe to call the renderPlain() method as suggested below:
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('node'); $build = $view_builder->view($node, $view_mode, $langcode); $html = \Drupal::service('renderer')->renderPlain($build);
Edit: But what about a full page?
If you intend to render not only the node content but a full page (from
</html>) you have to have the kernel handle an appropriate sub-request .