Skip to content

Commit

Permalink
Allowed specifying the partial name in the endpartial tag (#50)
Browse files Browse the repository at this point in the history
* Allowed specifying the partial name in the endpartial tag
* Shamelessly copy the Django sentence for the endpartial tag
  • Loading branch information
matthiask authored Aug 14, 2024
1 parent 9f89163 commit 9afe684
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# CHANGELOG

* Added official Django 5.1 support.
* Allowed adding the partial name to the `endpartialdef` tag, similar to how
`endblock` allows specifying the block name again.

## 24.2 (2024-04-08)

* Implemented ``reset()`` on the partial loader to pass down to child loaders
when the autoreloader detects a template change. This allows the cached loader
to be correctly cleared in development.
* Implemented ``reset()`` on the partial loader to pass down to child loaders
when the autoreloader detects a template change. This allows the cached loader
to be correctly cleared in development.

(The underlying issue here was masked prior to v24.1.)
(The underlying issue here was masked prior to v24.1.)

## 24.1 (2024-04-04)

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ TEST-PARTIAL-CONTENT
{% endpartialdef %}
```

For extra readability, you can optionally add the name to your `{% endblock %}` tag. For
example:

```html
{% load partials %}

{% partialdef test-partial %}
TEST-PARTIAL-CONTENT
{% endpartialdef test-partial %}
```

### Fragment Re-use

With the partial defined, you can reuse it multiple times later:
Expand Down
8 changes: 6 additions & 2 deletions src/template_partials/templatetags/partials.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def partialdef_func(parser, token):
Usage:
{% partialdef "partial_name" %}
{% partialdef partial_name %}
Partial content goes here
Expand Down Expand Up @@ -116,7 +116,11 @@ def _define_partial(parser, token, end_tag):
inline = False

# Parse the content until the end tag (`endpartialdef` or deprecated `endpartial`)
nodelist = parser.parse((end_tag,))
acceptable_endpartials = (end_tag, f"{end_tag} {partial_name}")
nodelist = parser.parse(acceptable_endpartials)
endpartial = parser.next_token()
if endpartial.contents not in acceptable_endpartials:
parser.invalid_block_tag(endpartial, "endpartial", acceptable_endpartials)
parser.delete_first_token()

if not hasattr(parser.origin, "partial_contents"):
Expand Down
43 changes: 37 additions & 6 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import django.template
from django.http import HttpResponse
from django.template import EngineHandler, engines
from django.template import EngineHandler, TemplateSyntaxError, engines
from django.template.loader import render_to_string
from django.test import TestCase, override_settings
from template_partials.apps import wrap_loaders
Expand Down Expand Up @@ -34,8 +34,8 @@ class PartialTagsTestCase(TestCase):
def test_partial_tags(self):
template = """
{% load partials %}
{% partialdef "testing-partial" %}HERE IS THE TEST CONTENT{% endpartialdef %}
{% partial "testing-partial" %}
{% partialdef testing-partial %}HERE IS THE TEST CONTENT{% endpartialdef %}
{% partial testing-partial %}
"""

# Compile and render the template
Expand All @@ -49,8 +49,8 @@ def test_partial_tags(self):
def test_deprecated_startpartial_tag(self):
template = """
{% load partials %}
{% startpartial "deprecated-testing-partial" %}DEPRECATED TEST CONTENT{% endpartial %}
{% partial "deprecated-testing-partial" %}
{% startpartial deprecated-testing-partial %}DEPRECATED TEST CONTENT{% endpartial %}
{% partial deprecated-testing-partial %}
"""

# Capture warnings
Expand All @@ -74,7 +74,7 @@ def test_deprecated_startpartial_tag(self):
def test_partialdef_tag_with_inline(self):
template = """
{% load partials %}
{% partialdef "testing-partial" inline=True %}
{% partialdef testing-partial inline=True %}
HERE IS THE TEST CONTENT
{% endpartialdef %}
"""
Expand All @@ -87,6 +87,37 @@ def test_partialdef_tag_with_inline(self):
# Check the rendered content
self.assertEqual("HERE IS THE TEST CONTENT", rendered.strip())

def test_endpartialdef_with_partial_name(self):
template = """
{% load partials %}
{% partialdef testing-partial %}
HERE IS THE TEST CONTENT
{% endpartialdef testing-partial %}
{% partial testing-partial %}
"""

# Compile and render the template
engine = engines["django"]
t = engine.from_string(template)
rendered = t.render()

# Check the rendered content
self.assertEqual("HERE IS THE TEST CONTENT", rendered.strip())

def test_endpartialdef_with_invalid_partial_name(self):
template = """
{% load partials %}
{% partialdef testing-partial %}
HERE IS THE TEST CONTENT
{% endpartialdef invalid %}
{% partial testing-partial %}
"""

with self.assertRaises(TemplateSyntaxError):
# Compile and render the template
engine = engines["django"]
engine.from_string(template)

def test_full_template_from_loader(self):
engine = engines["django"]
template = engine.get_template("example.html")
Expand Down

0 comments on commit 9afe684

Please sign in to comment.