Drupal 8 previously used Twig's filesystem and string loaders. Now any other Twig loader classes (implementing \Twig_LoaderInterface) can be added to a chain of Twig loaders via tagged services. The implementation is very similar to Symfony's. At least one Twig loader service is required at all times.
When searching for a template, the loader chain will use the first loader that doesn't throw a \Twig_Error_Loader exception. The optional 'priority' tag attribute controls the order in which loaders are executed and defaults to 0. The higher the priority, the sooner that loader gets executed.
This change allows contributed modules to do things like load templates from the database and provide a UI for editing Twig templates.
Example (twig_loader_test.services.yml):
services:
twig_loader_test.twig.test_loader:
class: Drupal\twig_loader_test\Loader\TestLoader
tags:
- { name: twig.loader, priority: -100 }