SSRと静的ファイル¶
テンプレートを配置する¶
標準ではapplication rootの templates/ を読み込みます。
描画する¶
@app.get("/")
def index():
return app.render_template(
"index.html",
title="higuma",
features=["Rust core", "Python API", "SSR"],
)
templates/index.html
{% extends "base.html" %}
{% block content %}
<h1>{{ title }}</h1>
<ul>
{% for feature in features %}
<li>{{ feature }}</li>
{% endfor %}
</ul>
{% endblock %}
MiniJinjaがRust側で描画し、text/html; charset=utf-8 として返します。
Context processor¶
すべてのtemplateへ共通値を追加できます。
静的ファイル¶
static_folder と static_url_path を指定できます。
静的ファイルではMIME type、Content-Length、ETag、If-None-Match、
Last-Modified、byte Rangeを処理します。path traversalは拒否されます。
Content-Type
HTMLを文字列以外のraw bytesで返す場合は、HTMLResponse または
Response(..., content_type="text/html; charset=utf-8") を指定してください。