diff --git a/falca/middleware/resource.py b/falca/middleware/resource.py index ce7a681..f03dc6a 100644 --- a/falca/middleware/resource.py +++ b/falca/middleware/resource.py @@ -15,7 +15,7 @@ def process_resource( ): if resource is not None: req.context.app = self.app - req.context.templates = self.app.template_lookup + req.context.template_engine = self.app.template_engine async def process_resource_async( self, req: ASGIRequest, resp: ASGIResponse, resource: object, *args diff --git a/falca/responses.py b/falca/responses.py index 9b82b97..818317b 100644 --- a/falca/responses.py +++ b/falca/responses.py @@ -88,7 +88,7 @@ def __init__(self, content: str, context: dict = {}, **kwds) -> None: super().__init__(content, **kwds) def render(self, req: Union[Request, ASGIRequest]): - t = req.context.templates.get_template(self.content) + t = req.context.template_engine.get_template(self.content) html = t.render(**self.context) return html diff --git a/falca/scaffold.py b/falca/scaffold.py index 3041d29..0aa4983 100644 --- a/falca/scaffold.py +++ b/falca/scaffold.py @@ -4,7 +4,6 @@ from falcon.asgi import App as ASGIApp from falcon.constants import MEDIA_JSON, WebSocketPayloadType -from mako.lookup import TemplateLookup from typer import Typer from . import handlers @@ -18,6 +17,7 @@ from .plugins.manager import PluginManager from .router import AsyncRouter, Router from .settings import Settings +from .templates import MakoTemplate class Scaffold: @@ -52,7 +52,7 @@ def __init__( templates.insert(0, os.path.join(os.path.dirname(__file__), "templates")) self.template_folders = templates - self.template_lookup = TemplateLookup(templates) + self.template_engine = self.get_template_engine(templates) self.plugins = self.plugins_class(self) for prefix, folder in static_folders: if not folder.startswith("/"): @@ -74,6 +74,9 @@ def __init__( self.add_middleware(FileParserMiddleware()) self._set_default_error_handlers() + def get_template_engine(self, templates: List[str]): + return MakoTemplate(templates) + def route(self, path: str, methods: List[str] = ["get", "head"]): def decorated(func): self._router.route(path, methods)(func) diff --git a/falca/templates.py b/falca/templates.py new file mode 100644 index 0000000..3f106bb --- /dev/null +++ b/falca/templates.py @@ -0,0 +1,18 @@ +from abc import ABCMeta, abstractmethod + +from mako.lookup import TemplateLookup +from six import with_metaclass + + +class TemplateEngine(with_metaclass(ABCMeta)): + @abstractmethod + def get_template(self, template: str): + pass + + +class MakoTemplate(TemplateEngine): + def __init__(self, *args, **kwds) -> None: + self.engine = TemplateLookup(*args, **kwds) + + def get_template(self, template: str): + return self.engine.get_template(template)