In Jinja2 template language, to use any statement, we need to put that statement in between curly brackets like this {% statement %} . And all statement must be ended with {% endstatement %} .

 

#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>
    {% if 'info' in data %}
      <p>{{data['info']|safe}}</p>
    {% else %}
      <p>No message here!!</p>
    {% endif %}
  </body>
</html>