Skip to content

Commit

Permalink
Address first pass review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaheedHaque committed Oct 12, 2023
1 parent 7e97489 commit 1a8b8a7
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/reactpy_django/templatetags/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
"""
Jinja support.
"""
import typing as t

from django.template import RequestContext, loader
from jinja2 import pass_context
from jinja2.ext import Extension
from jinja2.runtime import Context, Undefined

from .reactpy import component as djt_component
from .. import config
from jinja2.runtime import Context
from reactpy_django import config
from reactpy_django.templatetags.reactpy import component


class ReactPyExtension(Extension):
Expand All @@ -31,11 +28,11 @@ def __init__(self, environment):
#
# All we need is to add global "component" to the environment.
#
environment.globals["component"] = self._jinja_component
environment.globals["component"] = self.template_tag

@pass_context
def _jinja_component(self, __context: Context, dotted_path: str, *args: t.Any, host: str | None = None,
prerender: str = str(config.REACTPY_PRERENDER), **kwargs: t.Any) -> t.Union[t.Any, Undefined]:
def template_tag(self, jinja_context: Context, dotted_path: str, *args, host: str | None = None,
prerender: str = str(config.REACTPY_PRERENDER), **kwargs) -> str:
"""
This method is used to embed an existing ReactPy component into your
Jinja2 template.
Expand All @@ -58,10 +55,9 @@ def _jinja_component(self, __context: Context, dotted_path: str, *args: t.Any, h
Returns:
Whatever the components returns.
"""
djt_context = RequestContext(__context.parent['request'], autoescape=__context.eval_ctx.autoescape)
context = djt_component(djt_context, dotted_path, *args, host=host, prerender=prerender, **kwargs)
django_context = RequestContext(jinja_context.parent['request'], autoescape=jinja_context.eval_ctx.autoescape)
template_context = component(django_context, dotted_path, *args, host=host, prerender=prerender, **kwargs)
#
# TODO: can this be usefully cached?
#
result = loader.render_to_string(self.DJT_TEMPLATE, context, __context.parent['request'])
return result
return loader.render_to_string(self.DJT_TEMPLATE, template_context, jinja_context.parent['request'])

0 comments on commit 1a8b8a7

Please sign in to comment.