As in Python programming language, for statement in Jinja2 template language is to iterate over a variable that is a container object.
#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>
{% for key in data %}
<p>{{data[key]|safe}}</p>
{% endfor %}
</body>
</html>