Better support for AlpineJS #471
Replies: 4 comments 23 replies
-
For reference, django-web-components supports these characters: |
Beta Was this translation helpful? Give feedback.
-
I think this is a very valid use-case, passing things like I'm not sure about the "invalid attribute pass-through to attrs" solution though. I see that slippers and django-web-components have solved it this way. But I don't like that we're adding "magic" that transforms An easier way to follow this would be if attrs was right there as an attribute: {% component "icon"
icon="outline_trash"
attrs="@click.stop=\"$dispatch('user_delete', { role })\""
%}{% endcomponent %} (this would necessitate we don't mess up escaped quotes...) ... but I guess that limits the usability when you want to add more than one attribute. Maybe we could solve this with newlines? {% component "icon"
icon="outline_trash"
attrs="
@click.stop=\"$dispatch('user_delete', { role })\"
x-data=\"{ open: false }\"
"
%}{% endcomponent %} or with triple quotes? {% component "icon"
icon="outline_trash"
attrs="""
@click.stop="$dispatch('user_delete', { role })"
x-data="{ open: false }"
"""
%}{% endcomponent %} |
Beta Was this translation helpful? Give feedback.
-
So, let summarize the different options: A) Magic attrs (similar to Vue and django-web-components)
B) Allow special symbols and handle everything in get_context_data
C) Namespaced parameters
D) Triple quoted parameters (no special chars in params needed)
Did I miss any? |
Beta Was this translation helpful? Give feedback.
-
Hi all, this feature has just been released as v0.74 🎉 See the README section "Rendering HTML attributes" |
Beta Was this translation helpful? Give feedback.
-
Description
In my project, we use Django + AlpineJS, I've made a few changes that allow me to pass AlpineJS bindings through components more intuitively. Basically it's the same concept as Vue's passthrough/fallthrough attributes.
So I can pass AlpineJS syntax like
@click.stop
to my Django components:Whereas the
icon
component looks roughly something like this:3 things happen behind the scene in the example above:
@
,.
,-
. So@click.stop
is a valid key.icon
component, I check the names of all args and kwargs of theget_context_data()
method. All UNKNOWN kwargs get collected into anattrs
variable.attrs
variable to the top level<div>
, soWhat do you think?
Notes
For allowing extra special characters in kwargs, the inspiration comes from django-slippers, which allows
@.:-
in kwargs (see implementation). I suggest to add also#
to the mix.With recent introduction of
component_vars.is_filled
, theattrs
should be probably accessed viacomponent_vars.attrs
orcomponent_vars.fallthrough_attrs
.Similarly to Vue, it should be the component class that decides if it wants to use the fallthrough attrs feature or not. I suggest an opt-in, so that, by default, user gets an error if they pass in unknown args/kwargs.
NOTE: While Vue sets this via
inheritAttrs
, I thinkfallthrough_attrs
is more self-explanatory. (and googling forfallthrough attrs
also gets you to the Vue docs)This is the logic that extracts the "fallthrough attrs" from kwargs passed to
get_context_data()
So if I define
get_context_data
as:then
known_prop_names
is["name", "age", "city"]
and so if I render the component like so:
then
name
IS recognized, butdogs_count
is NOT recognized. Socomponent_vars.fallthrough_attrs
gets populated with {"dogs_count": 13}This bit from above:
could be done at class definition (
__new__
) so that we don't have to callinspect.getfullargspec
on every render.Implementation
Implementation consists of 2 sub-features:
Beta Was this translation helpful? Give feedback.
All reactions