-
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add minjinja * feat: add minjinja, more tests * feat: add minjinja, added url_for tests * feat: add minjinja, added context tests, seems we have something to deal with escapes * feat: add minjinja, added working csrf token tests * fix: not a fix but demonstrates that using | safe filter on url_for we can pass the builtin_functions tests, need to dig * fix: revert and dont use safe in tests * fix: change tests to mimic what minijinja would expect * fix: remove undeeded Environment config * fix: pin anyio because private imports broke CI * fix: add docs for minijinja fix: add tests for docs/examples/templating * fix: tests for running templates * fix: poetry lock * fix: anyio unpin * fix: some tests didnt use proper docs examples fix: seems mako prints a \n at end of templates if the html conatins one * fix: added more tabs for minijinja fix: typos in tabs names * fix: removed useless debug in mako examples * fix: auto rewite of the mako template by linters, tests will fail * fix: mako tests contains a \n at the end of the generated string --------- Co-authored-by: Na'aman Hirschfeld <[email protected]>
- Loading branch information
Showing
27 changed files
with
672 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
from litestar.contrib.jinja import JinjaTemplateEngine | ||
from litestar.template.config import TemplateConfig | ||
|
||
template_config = TemplateConfig(engine=JinjaTemplateEngine) | ||
template_config.engine_instance.engine.globals["foo"] = "bar" | ||
template_config = TemplateConfig(engine=JinjaTemplateEngine, directory="templates") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
from litestar.contrib.mako import MakoTemplateEngine | ||
from litestar.template.config import TemplateConfig | ||
|
||
template_config = TemplateConfig(engine=MakoTemplateEngine) | ||
template_config.engine_instance.engine.has_template("foo") | ||
template_config = TemplateConfig(engine=MakoTemplateEngine, directory="templates") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from litestar.contrib.minijnja import MiniJinjaTemplateEngine | ||
from litestar.template.config import TemplateConfig | ||
|
||
template_config = TemplateConfig(engine=MiniJinjaTemplateEngine, directory="templates") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from pathlib import Path | ||
|
||
from litestar import Litestar, get | ||
from litestar.contrib.minijnja import MiniJinjaTemplateEngine | ||
from litestar.response import Template | ||
from litestar.template.config import TemplateConfig | ||
|
||
|
||
@get(path="/") | ||
def index(name: str) -> Template: | ||
return Template(template_name="hello.html.minijinja", context={"name": name}) | ||
|
||
|
||
app = Litestar( | ||
route_handlers=[index], | ||
template_config=TemplateConfig( | ||
directory=Path(__file__).parent / "templates", | ||
engine=MiniJinjaTemplateEngine, | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from pathlib import Path | ||
|
||
from litestar import Litestar | ||
from litestar.contrib.minijnja import MiniJinjaTemplateEngine | ||
from litestar.template.config import TemplateConfig | ||
|
||
app = Litestar( | ||
route_handlers=[], | ||
template_config=TemplateConfig( | ||
directory=Path("templates"), | ||
engine=MiniJinjaTemplateEngine, | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import functools | ||
from pathlib import Path | ||
from typing import Any, Dict | ||
|
||
from litestar import Litestar, get | ||
from litestar.contrib.minijnja import MiniJinjaTemplateEngine, minijinja_from_state | ||
from litestar.response import Template | ||
from litestar.template.config import TemplateConfig | ||
|
||
|
||
def my_template_function(ctx: Dict[str, Any]) -> str: | ||
return ctx.get("my_context_key", "nope") | ||
|
||
|
||
def register_template_callables(engine: MiniJinjaTemplateEngine) -> None: | ||
engine.register_template_callable( | ||
key="check_context_key", | ||
template_callable=functools.partial(minijinja_from_state, my_template_function), | ||
) | ||
|
||
|
||
template_config = TemplateConfig( | ||
directory=Path(__file__).parent / "templates", | ||
engine=MiniJinjaTemplateEngine, | ||
engine_callback=register_template_callables, | ||
) | ||
|
||
|
||
@get("/") | ||
def index() -> Template: | ||
return Template(template_name="index.html.minijinja") | ||
|
||
|
||
app = Litestar(route_handlers=[index], template_config=template_config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello <strong>{{ name }}</strong> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<strong>check_context_key: </strong>{{ check_context_key() }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.