Skip to content

Commit

Permalink
🎨 Improved formatting: Make the template engine easy to customize (close
Browse files Browse the repository at this point in the history
 #14)
  • Loading branch information
aprilahijriyan committed Aug 26, 2021
1 parent 9ede4f6 commit 79096ca
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion falca/middleware/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion falca/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions falca/scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +17,7 @@
from .plugins.manager import PluginManager
from .router import AsyncRouter, Router
from .settings import Settings
from .templates import MakoTemplate


class Scaffold:
Expand Down Expand Up @@ -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("/"):
Expand All @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions falca/templates.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 79096ca

Please sign in to comment.