2
2
"""
3
3
Jinja support.
4
4
"""
5
- import typing as t
6
-
7
5
from django .template import RequestContext , loader
8
6
from jinja2 import pass_context
9
7
from jinja2 .ext import Extension
10
- from jinja2 .runtime import Context , Undefined
11
-
12
- from .reactpy import component as djt_component
13
- from .. import config
8
+ from jinja2 .runtime import Context
9
+ from reactpy_django import config
10
+ from reactpy_django .templatetags .reactpy import component
14
11
15
12
16
13
class ReactPyExtension (Extension ):
@@ -31,11 +28,10 @@ def __init__(self, environment):
31
28
#
32
29
# All we need is to add global "component" to the environment.
33
30
#
34
- environment .globals ["component" ] = self ._jinja_component
31
+ environment .globals ["component" ] = self .template_tag
35
32
36
33
@pass_context
37
- def _jinja_component (self , __context : Context , dotted_path : str , * args : t .Any , host : str | None = None ,
38
- prerender : str = str (config .REACTPY_PRERENDER ), ** kwargs : t .Any ) -> t .Union [t .Any , Undefined ]:
34
+ def template_tag (self , jinja_context : Context , dotted_path : str , * args , ** kwargs ) -> str :
39
35
"""
40
36
This method is used to embed an existing ReactPy component into your
41
37
Jinja2 template.
@@ -45,23 +41,14 @@ def _jinja_component(self, __context: Context, dotted_path: str, *args: t.Any, h
45
41
*args: The positional arguments to provide to the component.
46
42
47
43
Keyword Args:
48
- class: The HTML class to apply to the top-level component div.
49
- key: Force the component's root node to use a specific key value. \
50
- Using key within a template tag is effectively useless.
51
- host: The host to use for the ReactPy connections. If set to `None`, \
52
- the host will be automatically configured. \
53
- Example values include: `localhost:8000`, `example.com`, `example.com/subdir`
54
- prerender: Configures whether to pre-render this component, which \
55
- enables SEO compatibility and reduces perceived latency.
56
44
**kwargs: The keyword arguments to provide to the component.
57
45
58
46
Returns:
59
47
Whatever the components returns.
60
48
"""
61
- djt_context = RequestContext (__context .parent ['request' ], autoescape = __context .eval_ctx .autoescape )
62
- context = djt_component ( djt_context , dotted_path , * args , host = host , prerender = prerender , ** kwargs )
49
+ django_context = RequestContext (jinja_context .parent ['request' ], autoescape = jinja_context .eval_ctx .autoescape )
50
+ template_context = component ( django_context , dotted_path , * args , ** kwargs )
63
51
#
64
52
# TODO: can this be usefully cached?
65
53
#
66
- result = loader .render_to_string (self .DJT_TEMPLATE , context , __context .parent ['request' ])
67
- return result
54
+ return loader .render_to_string (self .DJT_TEMPLATE , template_context , jinja_context .parent ['request' ])
0 commit comments