diff --git a/README.md b/README.md index 9853866..c1cc197 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,16 @@ END {% endblock main %} ``` +You might want to wrap an existing part of your page, and continue rendering the content inside your partial, use the `inline` argument in that situation: + +```html +{% block main %} +{% startpartial inline-partial inline=True %} +CONTENT +{% endpartial %} +{% endblock main %} +``` + `django-template-partials` is also integrated with the template loader, so you can pass a template plus a partial name to the loader to have just that part rendered: ```python diff --git a/src/template_partials/templatetags/partials.py b/src/template_partials/templatetags/partials.py index c543699..2be8883 100644 --- a/src/template_partials/templatetags/partials.py +++ b/src/template_partials/templatetags/partials.py @@ -34,13 +34,17 @@ def render(self, context): class DefinePartialNode(template.Node): - def __init__(self, partial_name, nodelist): + def __init__(self, partial_name, inline, nodelist): self.partial_name = partial_name + self.inline = inline self.nodelist = nodelist def render(self, context): """Set content into context and return empty string""" - return "" + if self.inline: + return self.nodelist.render(context) + else: + return "" class RenderPartialNode(template.Node): @@ -72,15 +76,28 @@ def startpartial_func(parser, token): Stores the nodelist in the context under the key "partial_contents" and can be retrieved using the {% partial %} tag. + + The optional inline=True argument will render the contents of the partial + where it is defined. """ # Parse the tag - try: - tag_name, partial_name = token.split_contents() - except ValueError: + tokens = token.split_contents() + + # check we have the expected number of tokens before trying to assign them + # via indexes + if len(tokens) not in (2, 3): raise template.TemplateSyntaxError( - "%r tag requires a single argument" % token.contents.split()[0] + "%r tag requires 2-3 arguments" % token.contents.split()[0] ) + partial_name = tokens[1] + + try: + inline = tokens[2] + except IndexError: + # the inline argument is optional, so fallback to not using it + inline = False + # Parse the content until the {% endpartial %} tag nodelist = parser.parse(("endpartial",)) parser.delete_first_token() @@ -91,7 +108,7 @@ def startpartial_func(parser, token): nodelist, parser.origin, partial_name ) - return DefinePartialNode(partial_name, nodelist) + return DefinePartialNode(partial_name, inline, nodelist) # Define the partial tag to render the partial content. diff --git a/tests/templates/example.html b/tests/templates/example.html index a3cb504..21b226f 100644 --- a/tests/templates/example.html +++ b/tests/templates/example.html @@ -12,3 +12,7 @@ {% partial test-partial %} END {% endblock main %} + +{% startpartial inline-partial inline=True %} +INLINE-CONTENT +{% endpartial %} diff --git a/tests/tests.py b/tests/tests.py index 740399b..660dd3e 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -18,6 +18,22 @@ def test_partial_tags(self): # Check the rendered content self.assertEqual("HERE IS THE TEST CONTENT", rendered.strip()) + def test_startpartial_tag_with_inline(self): + template = """ + {% load partials %} + {% startpartial "testing-partial" inline=True %} + HERE IS THE TEST CONTENT + {% endpartial %} + """ + + # 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_full_template_from_loader(self): engine = engines["django"] template = engine.get_template("example.html") @@ -25,14 +41,19 @@ def test_full_template_from_loader(self): # Check the partial was rendered twice self.assertEqual(2, rendered.count("TEST-PARTIAL-CONTENT")) + self.assertEqual(1, rendered.count("INLINE-CONTENT")) def test_just_partial_from_loader(self): engine = engines["django"] + template = engine.get_template("example.html#test-partial") rendered = template.render() - self.assertEqual("TEST-PARTIAL-CONTENT", rendered.strip()) + template = engine.get_template("example.html#inline-partial") + rendered = template.render() + self.assertEqual("INLINE-CONTENT", rendered.strip()) + def test_debug_template(self): class LazyExceptionObject: def __str__(self):