mirror of https://github.com/encode/starlette.git
1.9 KiB
1.9 KiB
Starlette is not coupled to any particular templating engine, but Jinja2 provides an excellent choice.
Here we're going to take a look at a complete example of how you can configure a Jinja2 environment together with Starlette.
from starlette.applications import Starlette
from starlette.staticfiles import StaticFiles
from starlette.responses import HTMLResponse
def setup_jinja2(template_dir):
@jinja2.contextfunction
def url_for(context, name, **path_params):
request = context['request']
return request.url_for(name, **path_params)
loader = jinja2.FileSystemLoader(template_dir)
env = jinja2.Environment(loader=loader, autoescape=True)
env.globals['url_for'] = url_for
return env
env = setup_jinja2('templates')
app = Starlette(debug=True)
app.mount('/static', StaticFiles(directory='statics'), name='static')
@app.route('/')
async def homepage(request):
template = env.get_template('index.html')
content = template.render(request=request)
return HTMLResponse(content)
The important parts to note from the above example are:
- The StaticFiles app has been mounted with
name='static'
, meaning we can useapp.url_path_for('static', path=...)
orrequest.url_for('static', path=...)
. - The Jinja2 environment has a global
url_for
included, which allows us to useurl_for
inside our templates. We always need to pass the incomingrequest
instance in our context in order to be able to use theurl_for
function.
Asynchronous template rendering
Jinja2 supports async template rendering, however as a general rule we'd recommend that you keep your templates free from logic that invokes database lookups, or other I/O operations.
Instead we'd recommend that you ensure that your views perform all I/O, for example, strictly evaluate any database queries within the view and include the final results in the context.