3 namespace Drupal\Core\Template\Loader;
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\Extension\ThemeHandlerInterface;
9 * Loads templates from the filesystem.
11 * This loader adds module and theme template paths as namespaces to the Twig
12 * filesystem loader so that templates can be referenced by namespace, like
13 * @block/block.html.twig or @mytheme/page.html.twig.
15 class FilesystemLoader extends \Twig_Loader_Filesystem {
18 * Constructs a new FilesystemLoader object.
20 * @param string|array $paths
21 * A path or an array of paths to check for templates.
22 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
23 * The module handler service.
24 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
25 * The theme handler service.
27 public function __construct($paths, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
28 parent::__construct($paths);
30 // Add namespaced paths for modules and themes.
32 foreach ($module_handler->getModuleList() as $name => $extension) {
33 $namespaces[$name] = $extension->getPath();
35 foreach ($theme_handler->listInfo() as $name => $extension) {
36 $namespaces[$name] = $extension->getPath();
39 foreach ($namespaces as $name => $path) {
40 $this->addPath($path . '/templates', $name);
45 * Adds a path where templates are stored.
48 * A path where to look for templates.
49 * @param string $namespace
50 * (optional) A path name.
52 public function addPath($path, $namespace = self::MAIN_NAMESPACE) {
53 // Invalidate the cache.
55 $this->paths[$namespace][] = rtrim($path, '/\\');