-
-
Notifications
You must be signed in to change notification settings - Fork 23
Add Jinja support #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ShaheedHaque
wants to merge
4
commits into
reactive-python:main
Choose a base branch
from
ShaheedHaque:srh_jinja_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add Jinja support #199
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright © 2023 Innovatie Ltd. All rights reserved. | ||
""" | ||
Jinja support. | ||
""" | ||
import typing as t | ||
ShaheedHaque marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
ShaheedHaque marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from .. import config | ||
ShaheedHaque marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class ReactPyExtension(Extension): | ||
""" | ||
Jinja has more expressive power than core Django's templates, and can | ||
directly handle expansions such as: | ||
|
||
{{ component(*args, **kwargs) }} | ||
""" | ||
DJT_TEMPLATE = 'reactpy/component.html' | ||
ShaheedHaque marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# Therefore, there is no new tag to parse(). | ||
# | ||
tags = {} | ||
|
||
def __init__(self, environment): | ||
super().__init__(environment) | ||
# | ||
# All we need is to add global "component" to the environment. | ||
# | ||
environment.globals["component"] = self._jinja_component | ||
|
||
@pass_context | ||
def _jinja_component(self, __context: Context, dotted_path: str, *args: t.Any, host: str | None = None, | ||
ShaheedHaque marked this conversation as resolved.
Show resolved
Hide resolved
|
||
prerender: str = str(config.REACTPY_PRERENDER), **kwargs: t.Any) -> t.Union[t.Any, Undefined]: | ||
""" | ||
This method is used to embed an existing ReactPy component into your | ||
Jinja2 template. | ||
|
||
Args: | ||
dotted_path: String of the fully qualified name of a component. | ||
*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) | ||
ShaheedHaque marked this conversation as resolved.
Show resolved
Hide resolved
|
||
context = djt_component(djt_context, dotted_path, *args, host=host, prerender=prerender, **kwargs) | ||
Archmonger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# TODO: can this be usefully cached? | ||
# | ||
result = loader.render_to_string(self.DJT_TEMPLATE, context, __context.parent['request']) | ||
return result | ||
ShaheedHaque marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.