In twig_render_template(), I see this drupal_container()->get('twig')->loadTemplate($template_file)->render($variables). But I can not see where the twig service is defined.

  • 1
    I found it: \Drupal\Core\CoreServiceProvider::registerTwig() Sep 22, 2013 at 8:34

1 Answer 1


The class that registers the Twig service is CoreServiceProvider which, in CoreServiceProvider::register() (the method that every service provider needs to implement), uses the following code.

  // The 'request' scope and service enable services to depend on the Request
  // object and get reconstructed when the request object changes (e.g.,
  // during a subrequest).
  $container->addScope(new Scope('request'));

The class used for the Twig service is not the Twig_Environment class that Symfony implements, but a subclass of it (TwigEnvironment) implemented by Drupal.

The other registered service is twig.loader.filesystem, which uses the Symfony's Twig_Loader_Filesystem class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.