Skip to content

Commit

Permalink
Address second pass review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaheedHaque committed Nov 10, 2023
1 parent 7e97489 commit d432639
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 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,10 @@ 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, **kwargs) -> str:
"""
This method is used to embed an existing ReactPy component into your
Jinja2 template.
Expand All @@ -45,23 +41,14 @@ def _jinja_component(self, __context: Context, dotted_path: str, *args: t.Any, h
*args: The positional arguments to provide to the component.
Keyword Args:
class: The HTML class to apply to the top-level component div.
key: Force the component's root node to use a specific key value. \
Using key within a template tag is effectively useless.
host: The host to use for the ReactPy connections. If set to `None`, \
the host will be automatically configured. \
Example values include: `localhost:8000`, `example.com`, `example.com/subdir`
prerender: Configures whether to pre-render this component, which \
enables SEO compatibility and reduces perceived latency.
**kwargs: The keyword arguments to provide to the component.
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, **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 d432639

Please sign in to comment.