-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
21 changed files
with
487 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
Selva offers support for Jinja templates. | ||
|
||
To use templates, first install the `jinja` extra: | ||
|
||
```shell | ||
pip install selva[jinja] | ||
``` | ||
|
||
Template files are located in the `resources/templates directory`: | ||
|
||
``` | ||
project/ | ||
├── application/ | ||
│ └── ... | ||
└── resources/ | ||
└── templates/ | ||
├── index.html | ||
└── ... | ||
``` | ||
|
||
To render templates, inject the `selva.web.templates.Template` dependency and call its `respond` method: | ||
|
||
=== "application.py" | ||
|
||
```python | ||
from typing import Annotated | ||
from selva.di import Inject | ||
from selva.web import controller, get | ||
from selva.web.templates import Template | ||
|
||
|
||
@controller | ||
class Controller: | ||
template: Annotated[Template, Inject] | ||
|
||
@get | ||
async def index(self, request): | ||
context = {"title": "Index"} | ||
await self.template.respond(request.response, "index.html", context) | ||
``` | ||
|
||
=== "resources/templates/index.html" | ||
|
||
```html | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Jinja Templates</title> | ||
</head> | ||
<body> | ||
<h1>{{ title }}</h1> | ||
</body> | ||
</html> | ||
``` | ||
|
||
## Configuration | ||
|
||
Jinja can be configured through the `settings.yaml`. For exmaple, to activate extensions: | ||
|
||
```yaml | ||
templates: | ||
jinja: | ||
extensions: | ||
- jinja2.ext.i18n | ||
- jinja2.ext.debug | ||
``` | ||
Full settings list: | ||
```yaml | ||
templates: | ||
jinja: | ||
block_start_string: | ||
block_end_string: | ||
variable_start_string: | ||
variable_end_string: | ||
comment_start_string: | ||
comment_end_string: | ||
line_statement_prefix: | ||
line_comment_prefix: | ||
trim_blocks: | ||
lstrip_blocks: | ||
newline_sequence: | ||
keep_trailing_newline: | ||
extensions: | ||
optimized: | ||
undefined: | ||
finalize: | ||
autoescape: | ||
loader: | ||
cache_size: | ||
auto_reload: | ||
bytecode_cache: | ||
``` |
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 typing import Annotated | ||
|
||
from asgikit.requests import Request | ||
|
||
from selva.di import Inject | ||
from selva.web import controller, get | ||
from selva.web.templates import Template | ||
|
||
|
||
@controller | ||
class Controller: | ||
template: Annotated[Template, Inject] | ||
|
||
@get | ||
async def index( | ||
self, | ||
request: Request, | ||
): | ||
context = dict(title="Selva", heading="Heading") | ||
await self.template.respond(request.response, "index.html", context) |
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,5 @@ | ||
templates: | ||
jinja: | ||
extensions: | ||
- jinja2.ext.i18n | ||
- jinja2.ext.debug |
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 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>{{ title }}</title> | ||
</head> | ||
<body> | ||
<h1>{{ heading }}</h1> | ||
<pre> | ||
{% debug %} | ||
</pre> | ||
</body> | ||
</html> |
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 |
---|---|---|
|
@@ -45,6 +45,7 @@ nav: | |
- tutorial.md | ||
- controllers.md | ||
- routing.md | ||
- templates.md | ||
- configuration.md | ||
- middleware.md | ||
- logging.md |
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 |
---|---|---|
|
@@ -8,4 +8,9 @@ | |
"enable": [], | ||
"disable": [], | ||
}, | ||
"templates": { | ||
"jinja": { | ||
"path": "resources/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
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 @@ | ||
from selva.web.templates.template import Template |
Oops, something went wrong.