The include statement renders another template and outputs the result into the current template. The included template has access to context of the current template by default.
#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/head.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>
<!--templates/body.html-->
{% for info in data %}
<p>{{data[info]|safe}}</p>
{% endfor %}
<!--templates/foot.html-->
</body>
</html>
{% include 'header.html' %}
{% include 'body.html' %}
{% include 'footer.html' %}