Template RenderingΒΆ

Template rendering of FutureFinity uses the Jinja2 Template Engine, but FutureFinity replaces the TemplateLoader to load template from filesystem asynchronously (it takes the advantage of multithreading).

Let’s look at the example below:

from futurefinity.template import render_template

import futurefinity.web

import asyncio

app = futurefinity.web.Application(template_path="./template/",
                                   security_secret="__PUT_YOUR_SECRET_HERE__")


@app.add_handler("/")
class MainHandler(futurefinity.web.RequestHandler):
    async def get(self, *args, **kwargs):
        username = self.get_secure_cookie("username", default=None)
        if not username:
            return self.redirect("/login")

        return self.render("main.htm", template_dict={"name": username})


@app.add_handler("/login")
class LoginHandler(futurefinity.web.RequestHandler):
    @render_template("login.htm")
    async def get(self, *args, **kwargs):
        return

    async def post(self, *args, **kwargs):
        username = self.get_body_arg("username")
        self.set_secure_cookie("username", username)
        return self.redirect("/")

app.listen(23333)

try:
    asyncio.get_event_loop().run_forever()
except KeyboardInterrupt:
    pass

The example presents that FutureFinity supports two ways to render template. One is RequestHandler.render classmethod, and the other one is futurefinity.template.render_template decorator.

For the information about the Jinja2 Template, please visit: Jinja2 Documentation

If you want to override the template engine, you can just override ReuqestHandler.render_string method to switch to other template engine.