python / expert
Snippet
Django Custom Template Loader with Plugin Architecture
This snippet implements a plugin-based template loader architecture for Django. The `PluginLoader` coordinates multiple template plugins through a registry, each handling specific namespaces. Plugins implement `can_handle`, `fetch_source`, and `render_template` methods, enabling loading templates from databases, APIs, or custom storage systems while maintaining Django's standard template interface.
snippet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import osimport hashlibfrom django.template import TemplateDoesNotExistfrom django.template.loaders.base import Loaderfrom myapp.template_plugins import registryclass PluginLoader(Loader):def __init__(self, engine):super().__init__(engine)self.template_plugins = registry.get_template_sources()def get_template_sources(self, template_name):for plugin in self.template_plugins:if plugin.can_handle(template_name):source = plugin.fetch_source(template_name)if source:contents, path = sourceyield self._make_origin(path, template_name, plugin.name)def _make_origin(self, path, template_name, source_name):return f"{source_name}:{template_name}"def load_template(self, template_name, dirs=None):for origin in self.get_template_sources(template_name):try:contents = origin.split(':')[1] if ':' in origin else Nonefor plugin in self.template_plugins:if plugin.name == origin.split(':')[0]:template_string = plugin.render_template(template_name)return self.compile_template(template_string), originexcept Exception as e:continueraise TemplateDoesNotExist(template_name)class TemplatePlugin:def __init__(self, name, priority=50):self.name = nameself.priority = prioritydef can_handle(self, template_name):return template_name.startswith(f"{self.name}:")def fetch_source(self, template_name):raise NotImplementedErrordef render_template(self, template_name):raise NotImplementedError
django
Breakdown
1
class PluginLoader(Loader)
Custom Django template loader extending base Loader class
2
self.template_plugins = registry.get_template_sources()
Collects all registered plugins sorted by priority at initialization
3
def can_handle(self, template_name)
Plugin decides if it can handle given template name via namespace check
4
def fetch_source(self, template_name)
Plugin fetches raw template source from its storage backend
5
raise TemplateDoesNotExist(template_name)
Final exception when no plugin can supply the requested template