3

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() – Andy Truong Sep 22 '13 at 8:34
4

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'));
  $this->registerTwig($container);
  $this->registerModuleHandler($container);

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, privacy policy and cookie policy

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