Filter in Jinja2 language is a kind of function able to modify the variable next to it. There are many predefined filters in Jinja2 template engine, and we do not need to use all of them.

 

#routes/front/index.py
from sanic import Sanic
from sanic.response import html
from jinja2 import Environment,PackageLoader,select_autoescape

app = Sanic.get_app('Khmerweb')
app.static("/static", "static")
app.ctx.template = Environment(loader=PackageLoader("index"),autoescape=select_autoescape())

@app.route("/")
async def home(req):
    kdict = {}
    kdict['siteTitle'] = 'Khmer Web'
    kdict['pageTitle'] = 'Home Page'
    kdict['info'] = '<b>Welcome to Khmer Web Sanic!</b>'

    template = app.ctx.template.get_template("base.html")

    return html(template.render(data=kdict))

 

<!--templates/base.html-->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>{{data['siteTitle']}} | {{data['pageTitle']}}</title>
  </head>
  <body>
    <p>{{data['info']|safe}}</p>
  </body>
</html>