Skip to content

Commit

Permalink
Update the Jinja2Templates() constructor to allow PathLike (#1292)
Browse files Browse the repository at this point in the history
* Update the Jinja2Templates() to allow PathLike

* Update templating.py

* Update starlette/templating.py

Co-authored-by: Marcelo Trylesinski <[email protected]>

* Update starlette/templating.py

Co-authored-by: Marcelo Trylesinski <[email protected]>

* Update starlette/templating.py

Co-authored-by: Marcelo Trylesinski <[email protected]>

* Update starlette/templating.py

Co-authored-by: Marcelo Trylesinski <[email protected]>
  • Loading branch information
tonybaloney and Kludex authored Sep 26, 2021
1 parent 134eb0b commit 6c556f6
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions starlette/templating.py
Original file line number Diff line number Diff line change
@@ -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,13 @@ 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":
@pass_context
def url_for(context: dict, name: str, **path_params: typing.Any) -> str:
request = context["request"]
Expand Down

0 comments on commit 6c556f6

Please sign in to comment.