|
1 | 1 | import typing as t |
2 | | -from functools import lru_cache |
3 | 2 |
|
4 | | -import jinja2 |
5 | | -from ellar.common.templating import Environment |
| 3 | +from ellar.common.interfaces import ITemplateRenderingService |
6 | 4 | from starlette.background import BackgroundTask |
7 | 5 | from starlette.templating import _TemplateResponse as TemplateResponse |
8 | 6 |
|
9 | | -if t.TYPE_CHECKING: # pragma: no cover |
10 | | - from ellar.core.connection import Request |
11 | 7 |
|
12 | | - |
13 | | -@lru_cache(1200) |
14 | | -def get_template_name(template_name: str) -> str: |
15 | | - if not template_name.endswith(".html"): |
16 | | - return template_name + ".html" |
17 | | - return template_name |
18 | | - |
19 | | - |
20 | | -def process_view_model(view_response: t.Any) -> t.Dict: |
21 | | - if isinstance(view_response, dict): |
22 | | - return view_response |
23 | | - return {"model": view_response} |
24 | | - |
25 | | - |
26 | | -def _get_jinja_and_template_context( |
27 | | - template_name: str, request: "Request", **context: t.Any |
28 | | -) -> t.Tuple["jinja2.Template", t.Dict]: |
29 | | - jinja_environment = request.service_provider.get(Environment) |
30 | | - jinja_template = jinja_environment.get_template(get_template_name(template_name)) |
31 | | - template_context = dict(context) |
32 | | - template_context.update(request=request) |
33 | | - return jinja_template, template_context |
34 | | - |
35 | | - |
36 | | -def render_template_string( |
37 | | - template_string: str, request: "Request", **template_context: t.Any |
38 | | -) -> str: |
| 8 | +def render_template_string(template_string: str, **template_context: t.Any) -> str: |
39 | 9 | """Renders a template to string. |
40 | | - :param request: Request instance |
41 | 10 | :param template_string: Template String |
42 | 11 | :param template_context: variables that should be available in the context of the template. |
43 | 12 | """ |
44 | | - try: |
45 | | - jinja_template, template_context_ = _get_jinja_and_template_context( |
46 | | - template_name=template_string, |
47 | | - request=request, |
48 | | - **process_view_model(template_context), |
49 | | - ) |
50 | | - return jinja_template.render(template_context_) |
51 | | - except jinja2.TemplateNotFound: |
52 | | - jinja_environment = request.service_provider.get(Environment) |
53 | | - jinja_template = jinja_environment.from_string(template_string) |
| 13 | + from ellar.core.execution_context import current_injector |
54 | 14 |
|
55 | | - _template_context = dict(template_context) |
56 | | - _template_context.update(request=request) |
57 | | - |
58 | | - return jinja_template.render(_template_context) |
| 15 | + rendering_service: ITemplateRenderingService = current_injector.get( |
| 16 | + ITemplateRenderingService |
| 17 | + ) |
| 18 | + return rendering_service.render_template_string( |
| 19 | + template_string=template_string, **template_context |
| 20 | + ) |
59 | 21 |
|
60 | 22 |
|
61 | 23 | def render_template( |
62 | 24 | template_name: str, |
63 | | - request: "Request", |
64 | 25 | background: t.Optional[BackgroundTask] = None, |
| 26 | + headers: t.Union[t.Mapping[str, str], None] = None, |
65 | 27 | status_code: int = 200, |
66 | 28 | **template_kwargs: t.Any, |
67 | 29 | ) -> TemplateResponse: |
68 | 30 | """Renders a template from the template folder with the given context. |
69 | 31 | :param status_code: Template Response status code |
70 | | - :param request: Request instance |
71 | 32 | :param template_name: the name of the template to be rendered |
| 33 | + :param headers: Response Headers |
72 | 34 | :param template_kwargs: variables that should be available in the context of the template. |
73 | 35 | :param background: any background task to be executed after render. |
74 | 36 | :return TemplateResponse |
75 | 37 | """ |
76 | | - jinja_template, template_context = _get_jinja_and_template_context( |
77 | | - template_name=template_name, |
78 | | - request=request, |
79 | | - **process_view_model(template_kwargs), |
| 38 | + from ellar.core.execution_context import current_injector |
| 39 | + |
| 40 | + rendering_service: ITemplateRenderingService = current_injector.get( |
| 41 | + ITemplateRenderingService |
80 | 42 | ) |
81 | | - return TemplateResponse( |
82 | | - template=jinja_template, |
83 | | - context=template_context, |
| 43 | + return rendering_service.render_template( |
| 44 | + template_name=template_name, |
| 45 | + template_context=template_kwargs, |
84 | 46 | background=background, |
85 | 47 | status_code=status_code, |
| 48 | + headers=headers, |
86 | 49 | ) |
0 commit comments