Skip to content

Commit

Permalink
Merge pull request #6 from SmileyChris/fix-component-vars
Browse files Browse the repository at this point in the history
Fix component variables not getting parsed correctly.
  • Loading branch information
SmileyChris authored Jul 25, 2024
2 parents a6943c0 + b21429c commit 02c4edb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/5.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where the component context wasn't being set correctly, especially noticeable inside of a loop.
1 change: 1 addition & 0 deletions includecontents/templatetags/includecontents.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def set_component_attrs(self, context: Context, new_context: Context):
# Don't use the extra context for the include tag if it's a component
# since we've handled adding it to the new context ourselves.
extra_context = self.include_node.extra_context
self.include_node.extra_context = {}
yield
self.include_node.extra_context = extra_context

Expand Down
6 changes: 6 additions & 0 deletions tests/templates/test_component/multi.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<main>
<include:card title="hello">Title</include:card>
{% for i in "123" %}
<include:card title={i}>loop {{ i }}</include:card>
{% endfor %}
</main>
29 changes: 29 additions & 0 deletions tests/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,32 @@ def test_shorthand_attrs():
Context({"food": "pizza"})
)
assert output == "pizza"


def test_multi():
output = render_to_string("test_component/multi.html")
assert output == (
"""<main>
<section class="card">
<h3 >hello</h3>
<div>Title</div>
</section>
<section class="card">
<h3 >1</h3>
<div>loop 1</div>
</section>
<section class="card">
<h3 >2</h3>
<div>loop 2</div>
</section>
<section class="card">
<h3 >3</h3>
<div>loop 3</div>
</section>
</main>
"""
)

0 comments on commit 02c4edb

Please sign in to comment.