Add Jinja support#199
Conversation
54906cf to
1384632
Compare
|
OK, this now render the "Hello World!" correctly. Feedback requested before thinking about tests/docs etc. |
1384632 to
2d282e4
Compare
2d282e4 to
7e97489
Compare
1a8b8a7 to
d432639
Compare
|
Resolved last outstanding comment. And ran black. If we are to proceed, I'd propose to squash the current commits before thinking about tests/packaging/docs etc. (FWIW, in that regard, I assume the appropriate packaging would be as something like "reactpy_django[jinja]"...but I'm not currently sure how to go about implementing that). |
|
Optional dependencies can be defined within the package in setup.py. For example: {
"extras_require": {
"encryption": ["cryptography", "pycryptodome"],
},
...
} |
Thanks, I'm more or less familiar with this bit. I was more thinking about the new file I have added (and any supporting test file), and whether that could/should be omitted unless "[jinja] was specified? |
|
I have some changes in one of my PRs that will simplify test configuration TLDR: Our test suite can now run multiple different Django settings.py files. And yes, from the user's perspective we should have all the jinja dependencies be optional via |
|
My PR has been merged. All There shouldn't be anything blocking this PR anymore, so you'll need to do the following:
|
|
Noted. I've a full plate right now but have this on my radar. |
- Created `ReactPyExtension` Jinja2 extension with `component`,
`pyscript_component`, and `pyscript_setup` globals that delegate to the
existing Django template tag implementations.
- Refactored `reactpy.py` to export template name constants
(COMPONENT_TEMPLATE, PYSCRIPT_COMPONENT_TEMPLATE, PYSCRIPT_SETUP_TEMPLATE)
so they can be reused by the Jinja2 extension.
- Added JINJA_COMPONENT_REGEX to utils.py's RootComponentFinder so that
{{ component(...) }} syntax in Jinja2 templates is auto-detected.
- Added test infrastructure: settings_jinja.py, jinja_env.py, jinja_views.py,
jinja_urls.py, Jinja2 template files, and test_jinja.py.
- Made test_app/__init__.py gracefully handle missing `bun` binary by
skipping the JS rebuild when artifacts already exist.
- Added jinja2 to pyproject.toml hatch-test extra-dependencies.
- Updated CHANGELOG.md.
(cherry picked from commit 3923cee)
- Remove invalid 'jinja2' entry from INSTALLED_APPS in settings_jinja.py (jinja2 is a Python library, not a Django app) - Consolidate imports from reactpy_django.templatetags.reactpy into a single import block in jinja.py - Add [project.optional-dependencies] jinja section to pyproject.toml so 'pip install reactpy_django[jinja]' works for end users - Strengthen test_jinja.py assertions and remove empty view test stubs that provided no coverage
- Run ruff format fix on test_jinja.py (single-line .from_string call) - Run ruff check --fix on jinja.py (sorted imports per project convention)
The Jinja wrapper used prerender="" as default, but str_to_bool("")
raises ValueError. Fix by matching the Django tag behavior — default to
str(REACTPY_PRERENDER) when no explicit prerender value is provided.
The component template does not use data-reactpy attribute. Check for mountComponent and the component dotted path instead.
- Remove [project.optional-dependencies] jinja = ["jinja2"] from pyproject.toml - Add try/except ModuleNotFoundError guard at the top of jinja.py with a helpful error message telling users to install jinja2 - Create docs/src/learn/add-reactpy-to-a-django-project-with-jinja.md with Jinja2-specific setup instructions - Update mkdocs.yml nav to include the new Jinja page under Get Started
Description
WORK IN PROGRESS: DO NOT MERGE.
This is incomplete support for Jinja2-based templates. I could use some pointers to finish the missing part (see comments at the end of src/reactpy_django/templatetags/jinja.py).(also missing, docs, packaging and tests)
To reproduce the results to date requires the following example changes on the Django side...
Configure template files ending in ".jinja" to be processed via Jinja:
Add the new app:
File of components in
components.py:Test view in
views/react.py:Template for the view
templates/my-template.html.jinja:project/asgi.pyAnd finally
project/urls.py:Once Django is restarted, navigating to the view, you should see
Hello World!Checklist:
Please update this checklist as you complete each item:
By submitting this pull request you agree that all contributions comply with this project's open source license(s).