Skip to content

Patch TemplateProxy to restore compatibility with template_partials #1328

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
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Aug 7, 2025

This PR implements TemplateProxy patching to restore compatibility between django-components and template_partials, as suggested by @carltongibson in issue #1323.

Problem

When both django-components and template_partials are used together, the TemplateProxy class from template_partials doesn't receive the same monkey-patching that django-components applies to Django's Template class. This can lead to compatibility issues with component context handling and dependency rendering.

Solution

The fix adds conditional patching of TemplateProxy when template_partials is detected in INSTALLED_APPS:

  1. Enhanced monkey-patching system: Added monkeypatch_template_proxy_cls() and monkeypatch_template_proxy_render() functions that apply the same context handling and dependency rendering logic to TemplateProxy as is applied to Django's Template class.

  2. Conditional activation: Modified ComponentsConfig.ready() to check for "template_partials" in settings.INSTALLED_APPS and automatically patch TemplateProxy if present.

  3. Safe error handling: The implementation gracefully handles cases where template_partials is listed in INSTALLED_APPS but not actually installed.

Key Features

  • Seamless integration: No changes required from users - the patching happens automatically when both packages are installed
  • Component context compatibility: TemplateProxy now properly handles component nesting and isolation contexts
  • Dependency rendering: Template partials now participate in django-components' CSS/JS dependency system
  • Backward compatible: Existing functionality remains unchanged when template_partials is not used

Example Usage

# In settings.py
INSTALLED_APPS = [
    'django_components',
    'template_partials',  # TemplateProxy will be automatically patched
    # ...
]
<!-- Templates can now use both systems together -->
{% load component_tags %}
{% load partials %}

{% partialdef header %}
<header>Shared header</header>
{% endpartialdef %}

{% component "my_component" %}{% endcomponent %}
{% partial header %}

The patching ensures that template partials work correctly within component contexts and that any CSS/JS dependencies are properly rendered.

Fixes #1327.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copy link
Author

Copilot AI commented Aug 7, 2025

@EmilStenstrom 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Co-authored-by: EmilStenstrom <224130+EmilStenstrom@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Patch TemplateProxy to restore compatibility with template_partials Patch TemplateProxy to restore compatibility with template_partials Aug 7, 2025
Copilot finished work on behalf of EmilStenstrom August 7, 2025 20:35
@Copilot Copilot AI requested a review from EmilStenstrom August 7, 2025 20:35
@EmilStenstrom EmilStenstrom requested review from JuroOravec and removed request for EmilStenstrom August 7, 2025 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Patch TemplateProxy to restore compatibility with template_partials
2 participants