22"""
33Jinja support.
44"""
5- import typing as t
6-
75from django .template import RequestContext , loader
86from jinja2 import pass_context
97from 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
1411
1512
1613class ReactPyExtension (Extension ):
@@ -31,11 +28,11 @@ def __init__(self, environment):
3128 #
3229 # All we need is to add global "component" to the environment.
3330 #
34- environment .globals ["component" ] = self ._jinja_component
31+ environment .globals ["component" ] = self .template_tag
3532
3633 @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 , host : str | None = None ,
35+ prerender : str = str (config .REACTPY_PRERENDER ), ** kwargs ) -> str :
3936 """
4037 This method is used to embed an existing ReactPy component into your
4138 Jinja2 template.
@@ -58,10 +55,9 @@ def _jinja_component(self, __context: Context, dotted_path: str, *args: t.Any, h
5855 Returns:
5956 Whatever the components returns.
6057 """
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 )
58+ django_context = RequestContext (jinja_context .parent ['request' ], autoescape = jinja_context .eval_ctx .autoescape )
59+ template_context = component ( django_context , dotted_path , * args , host = host , prerender = prerender , ** kwargs )
6360 #
6461 # TODO: can this be usefully cached?
6562 #
66- result = loader .render_to_string (self .DJT_TEMPLATE , context , __context .parent ['request' ])
67- return result
63+ return loader .render_to_string (self .DJT_TEMPLATE , template_context , jinja_context .parent ['request' ])
0 commit comments