-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
feat: Scoped slots + Updated docs #495
feat: Scoped slots + Updated docs #495
Conversation
for more information, see https://pre-commit.ci
@@ -433,20 +442,3 @@ def render(self, context: Context) -> str: | |||
|
|||
trace_msg("RENDR", "COMP", self.name_fexp, self.component_id, "...Done!") | |||
return output | |||
|
|||
|
|||
def safe_resolve_list(args: List[FilterExpression], context: Context) -> List: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved functions that work with FilterExpression into file expression.py
return HtmlAttrsNode(attributes, default_attrs, append_attrs) | ||
|
||
|
||
def is_whitespace_node(node: Node) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These three is_whitespace_node
, is_whitespace_token
and is_block_tag_token
weren't being used. Actually, even in the MR that introduced them didn't use them. Since it's not documented, I assume it's not part of the public API.
@@ -393,7 +469,3 @@ def _get_positional_param( | |||
|
|||
def is_wrapped_in_quotes(s: str) -> bool: | |||
return s.startswith(('"', "'")) and s[0] == s[-1] | |||
|
|||
|
|||
def strip_quotes(s: str) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of stripping quotes outselves, I used FilterExpression.resolve()
to do that for us here https://github.com/EmilStenstrom/django-components/pull/495/files#diff-7345171ed38e1dd2e4b8199b65a2fd082323bdb331ff0ef0dc0dd3bd43f111aeR345
@@ -237,19 +237,43 @@ class Calendar(component.Component): | |||
|
|||
And voilá!! We've created our first component. | |||
|
|||
## Autodiscovery | |||
## Using single-file components |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the section on "single-file components" and "components as Views" higher up. Might not be easy to see in this diff, and just my personal opinion, but it feels to me that now README flows even a bit better:
- installation
- creating components (and the different variants thereof)
- registering componnt with autodiscovery
- using slots and passing data to components (which requires the other component to be registered)
- Passing data to components
- and them some more niche stuff
Overall the ordering is:
@@ -460,86 +532,88 @@ This is fine too: | |||
{% endcomponent %} | |||
``` | |||
|
|||
### Components as views | |||
### Render fill in multiple places |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this section since it wasn't documented yet.
|
||
To be able to access a slot name via `component_vars.is_filled`, the slot name needs to be composed of only alphanumeric characters and underscores (e.g. `this__isvalid_123`). | ||
|
||
However, you can still define slots with other special characters. In such case, the slot name in `component_vars.is_filled` is modified to replace all invalid characters into `_`. | ||
|
||
So a slot named `"my super-slot :)"` will be available as `component_vars.is_filled.my_super_slot___`. | ||
|
||
### Setting Up `ComponentDependencyMiddleware` | ||
### Scoped slots |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added section on scoped slots and moved ComponentDependencyMiddleware
further down to its own section on rendering dependencies.
@@ -1175,6 +1349,40 @@ NOTE: `{% csrf_token %}` tags need access to the top-level context, and they wil | |||
|
|||
Components can also access the outer context in their context methods by accessing the property `outer_context`. | |||
|
|||
## Rendering JS and CSS dependencies |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the section on middleware here, as I expect that this section will be expanded when we tackle #478
@@ -77,6 +81,7 @@ class SlotFill(NamedTuple): | |||
nodelist: NodeList | |||
context_data: Dict | |||
alias: Optional[AliasName] | |||
scope: Optional[ScopeName] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How the feature is implemented is that when we parse slot
tags, we note down the kwargs parsed to it, and pass it down. Similarly, when we parse fill
tags, we note down the slot_data
variable name, and pass it down. These two then meet when we are rendering a slot, and we assign the slot kwargs to the context under a key set in fill
tag.
slot_data
value is required to be string literal, e.g. "quoted_string"
, same as we do for fill name or the as var
syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks great. I thought the API was that you set the names of your parameters in the slot tag, but now it takes a data param with a dict instead?
2819057
to
5b78e34
Compare
I also added a table of contents to README |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really good work on this!
I see no tests with context_behavior="isolated", so I guess that all of them are testing "django"?
No, |
Btw, @EmilStenstrom, what do you think of renaming the kwarg through which we access the data from When we take also #502 into consideration, then IMO the shorter names would make for a cleaner API. Consider: {% fill "my_slot" data="slot_data" default="slot_default" %} vs {% fill "my_slot" slot_data="slot_data" slot_default="slot_default" %} |
@JuroOravec Definitely like the data variable better. |
@EmilStenstrom Thanks again for your time and for all the feedback! I've changed |
Implementation may still change based on feedback.This MR is now ready for review, it adds "scoped slot" feature as per #494 (comment).I also moved the README sections around a bit, and added a view missing sections. See the screenshot in #495 (comment) and other comments below.
Closes #494