Skip to content

Commit

Permalink
Address lastoutstadnngcomment, and run black.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaheedHaque committed Nov 26, 2023
1 parent d432639 commit d888b4d
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/reactpy_django/templatetags/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
from reactpy_django import config
from reactpy_django.templatetags.reactpy import component

#
# Point to our non-Django analogue.
#
DJT_TEMPLATE = "reactpy/component.html"

This comment has been minimized.

Copy link
@Archmonger

Archmonger Nov 27, 2023

Contributor

This does not solve the code duplication issue.

This should be stored within reactpy_django.templatetags.reactpy and imported into this file.

References to "reactpy/component.html" within reactpy_django.templatetags.reactpy will need to be replaced with this global.



class ReactPyExtension(Extension):
"""
Expand All @@ -17,7 +22,7 @@ class ReactPyExtension(Extension):
{{ component(*args, **kwargs) }}
"""
DJT_TEMPLATE = 'reactpy/component.html'

#
# Therefore, there is no new tag to parse().
#
Expand All @@ -31,7 +36,9 @@ def __init__(self, environment):
environment.globals["component"] = self.template_tag

@pass_context
def template_tag(self, jinja_context: Context, dotted_path: str, *args, **kwargs) -> str:
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 @@ -46,9 +53,14 @@ def template_tag(self, jinja_context: Context, dotted_path: str, *args, **kwargs
Returns:
Whatever the components returns.
"""
django_context = RequestContext(jinja_context.parent['request'], autoescape=jinja_context.eval_ctx.autoescape)
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?
#
return loader.render_to_string(self.DJT_TEMPLATE, template_context, jinja_context.parent['request'])
return loader.render_to_string(
DJT_TEMPLATE, template_context, jinja_context.parent["request"]
)

0 comments on commit d888b4d

Please sign in to comment.