Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the Jinja2Templates() constructor to allow PathLike #1292

Merged
merged 6 commits into from Sep 26, 2021
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions starlette/templating.py
@@ -1,4 +1,5 @@
import typing
from os import PathLike

from starlette.background import BackgroundTask
from starlette.responses import Response
Expand Down Expand Up @@ -54,11 +55,11 @@ class Jinja2Templates:
return templates.TemplateResponse("index.html", {"request": request})
"""

def __init__(self, directory: str) -> None:
def __init__(self, directory: typing.Union[str, PathLike]) -> None:
assert jinja2 is not None, "jinja2 must be installed to use Jinja2Templates"
self.env = self._create_env(directory)

def _create_env(self, directory: str) -> "jinja2.Environment":
def _create_env(self, directory: typing.Union[str, PathLike]) -> "jinja2.Environment":
Kludex marked this conversation as resolved.
Show resolved Hide resolved
@pass_context
def url_for(context: dict, name: str, **path_params: typing.Any) -> str:
request = context["request"]
Expand Down