diff --git a/README.md b/README.md index d1feeaa..f4c37fd 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ - Automatic Ping / Pong Support - Per Socket Data - Middlewares +- Templates Support (examples with [Mako](https://github.com/cirospaciari/socketify.py/tree/main/examples/template_mako.py) and [Jinja2)](https://github.com/cirospaciari/socketify.py/tree/main/examples/template_jinja2.py)) ## :mag_right: Upcoming Features - Fetch like API powered by libuv - Async file IO powered by libuv diff --git a/examples/helpers/templates.py b/examples/helpers/templates.py new file mode 100644 index 0000000..698069d --- /dev/null +++ b/examples/helpers/templates.py @@ -0,0 +1,31 @@ +#Simple example of mako and jinja2 template plugin for socketify.py +from mako.template import Template +from mako.lookup import TemplateLookup +from mako import exceptions + + +from jinja2 import Environment, FileSystemLoader + +class Jinja2Template: + def __init__(self, searchpath, encoding='utf-8', followlinks=False): + self.env = Environment(loader=FileSystemLoader(searchpath, encoding, followlinks)) + + #You can also add caching and logging strategy here if you want ;) + def render(self, templatename, **kwargs): + try: + template = self.env.get_template(templatename) + return template.render(**kwargs) + except Exception as err: + return str(err) + +class MakoTemplate: + def __init__(self, **options): + self.lookup = TemplateLookup(**options) + + #You can also add caching and logging strategy here if you want ;) + def render(self, templatename, **kwargs): + try: + template = self.lookup.get_template(templatename) + return template.render(**kwargs) + except Exception as err: + return exceptions.html_error_template().render() \ No newline at end of file diff --git a/examples/requeriments.txt b/examples/requeriments.txt index aeca5c7..a5df884 100644 --- a/examples/requeriments.txt +++ b/examples/requeriments.txt @@ -3,4 +3,5 @@ aiofiles aiofile redis strawberry-graphql +mako git+https://github.com/cirospaciari/socketify.py.git@main#socketify --global-option="build_ext" \ No newline at end of file diff --git a/examples/template_jinja2.py b/examples/template_jinja2.py new file mode 100644 index 0000000..c91ba23 --- /dev/null +++ b/examples/template_jinja2.py @@ -0,0 +1,14 @@ +from socketify import App +#see helper/templates.py for plugin implementation +from helpers.templates import Jinja2Template + + +app = App() +app.template(Jinja2Template("./templates", encoding='utf-8', followlinks=False)) + +async def home(res, req): + res.render("jinja2_home.html", title="Hello", message="Hello, World") + +app.get("/", home) +app.listen(3000, lambda config: print("Listening on port http://localhost:%s now\n" % str(config.port))) +app.run() \ No newline at end of file diff --git a/examples/template_mako.py b/examples/template_mako.py new file mode 100644 index 0000000..7cabb24 --- /dev/null +++ b/examples/template_mako.py @@ -0,0 +1,14 @@ +from socketify import App +#see helper/templates.py for plugin implementation +from helpers.templates import MakoTemplate + + +app = App() +app.template(MakoTemplate(directories=['./templates'], output_encoding='utf-8', encoding_errors='replace')) + +async def home(res, req): + res.render("mako_home.html", message="Hello, World") + +app.get("/", home) +app.listen(3000, lambda config: print("Listening on port http://localhost:%s now\n" % str(config.port))) +app.run() \ No newline at end of file diff --git a/examples/templates/jinja2_base.html b/examples/templates/jinja2_base.html new file mode 100644 index 0000000..2fe47ef --- /dev/null +++ b/examples/templates/jinja2_base.html @@ -0,0 +1,20 @@ + + +
+this is the body content. {{ message }}
+{% endblock body %} \ No newline at end of file diff --git a/examples/templates/mako_base.html b/examples/templates/mako_base.html new file mode 100644 index 0000000..215ced0 --- /dev/null +++ b/examples/templates/mako_base.html @@ -0,0 +1,15 @@ + + +