Skip to content

Improved slot handling - #56

Merged
EmilStenstrom merged 10 commits into
django-components:masterfrom
rbeard0330:master
May 25, 2021
Merged

EmilStenstrom merged 10 commits into
django-components:masterfrom
rbeard0330:master

Conversation

@rbeard0330

Copy link
Copy Markdown
Contributor

Emil,

This should fix #34 and #37. The general approach is to allow SlotNodes to render themselves, instead of removing them from the template and replacing them with the correct nodelist at the time the template is compiled. Now, the first time the component is rendered, it loads its template, searches through all the nodes in the template (including through descendants), and gives each SlotNode a reference to itself (i.e., the parent component). Then, when it is time for the SlotNode to render itself, it checks to see if it has a parent reference and, if it does, if the parent has an overriding nodelist for it to use. If an override exists, it renders that, otherwise it renders its default nodelist.

I think it should be possible to extend this approach to handle #33 (slot.super()) as well, but I haven't fully thought that through yet.

As always, let me know of comments/questions/suggestions.

Best,
Robert

@rbeard0330

Copy link
Copy Markdown
Contributor Author

I just pushed a couple additional commits. These address two issues:

  1. I started encountering performance problems with the test suite. Some investigation showed that the deepcopy of the template nodelist in Component.get_processed_template() was picking up a node that had a reference to the whole template engine. Since we only need to copy the nodelists, I wrote a helper function to recursively copy the nodes in the nodelist, along with their subsidiary nodelists.

  2. I also added an implementation of {{ slot.super }}. To avoid concurrency issues, a new SlotNode is instantiated for each render, and it's receives a reference to the context for the render. Then the new SlotNode can use the saved context and its default nodelist to respond to super calls.

@EmilStenstrom

Copy link
Copy Markdown
Collaborator

THIS IS FANTASTIC!!! :D Thank you so much for this. Give me a couple of days to go through the changes you made. You made my day! :)

@EmilStenstrom EmilStenstrom left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I've finally looked through it! I added some questions and minor things, but overall this looks great! Mind having a look so we can get this merged? :)

Comment thread django_components/component.py Outdated
"""Retrieve the requested template and add a link to this component to each SlotNode in the template."""

source_template = get_template(template_name)
component_template = copy(source_template)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed? Removing it does not break any test, and all accesses below seem to be copying subparts of the template anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is, but let me think about this and try to write a test to capture the problem if there is one.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rbeard0330 I don't want to rush, is this conversation still unresolved, or did your commit at the end resolve this? I don't know if you are waiting for me to check again or not :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All set. Just pushed some commits with the new test and some slight tweaking of this section.

Comment thread django_components/component.py Resolved
Comment thread tests/test_templatetags.py Resolved
Comment thread tests/test_templatetags.py Resolved
@EmilStenstrom
EmilStenstrom merged commit 5c1dff7 into django-components:master May 25, 2021
@EmilStenstrom

Copy link
Copy Markdown
Collaborator

Looks great! Thanks again for your hard work. I'll merge this right now and release version 0.13 🥳 🎊 🎆

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.

Nested slots don't work

2 participants