Replies: 8 comments 16 replies
-
We wrote a home-baked version of this at $WORK, and I can confirm that it's an absolute killer feature I can't live without. +1 |
Beta Was this translation helpful? Give feedback.
-
Hey @wrabit , Adding a middleware to every request adds some overhead when it may not be needed. Here are a few different ideas to noodle on.
|
Beta Was this translation helpful? Give feedback.
-
To be honest I don't think this is something I personally can find a use case for. By design I always avoid inline js and scripts linking. I prefer to use the tailwindcss+alpine+htmx stack, so the literal only thing I'm writing is python and django template html. (I've tried a large number of different things, and this is by far my favorite. Typically I'll just bring in all my static assets in
is something I personally avoid, and just use alpine or htmx to handle via pure html. |
Beta Was this translation helpful? Give feedback.
-
Also to the point
In my mind, encapsulation isnt as clear as it could be as the behavior is decoupled. Which is why id prefer alpinejs (or htmx) here -- locality of behavior is more preserved. https://htmx.org/essays/locality-of-behaviour/ Just for completeness, here's how I might add a tooltip to a button.
And of course with cotton, you could break this into components. The main benefit is the business logic is very clear (my_view.html) and you can trivially place tooltip buttons around everywhere ( All the tooltip "behavior" is in one file, button.html. my_view.html
button.html
tooltip.html
|
Beta Was this translation helpful? Give feedback.
-
For what it's worth, here is an example of how we use our similar version (which is based on custom template tags):
And then somewhere in a base template:
A couple nice things about this:
In terms of performance overhead, our naive implementation is based on middleware and takes ~dozens microseconds to run, which is likely fast enough for most people using Django. |
Beta Was this translation helpful? Give feedback.
-
I have times where I reach for both of these patterns. In one app we use compressor for the main assets, we do have a couple of Alpine.data()'s outside of that, but for the requirements of this app it's fine. In this case I would reach for push stacks to keep the definition close to the component. In other MVP style apps where performance is not critical / load is very small, this way of working allows you to have a system in place where you can send things to head or footer without little hassle and is highly maintainable, more reasoning of the component stays in the component. Then there is building and maintaining re-usable components where all things you need to manage are in the one file. For example a combobox we use a lot and share amongst projects would benefit the push stack to make it more 'plug and play'. So all fair points and there of course is not one single way. Benchmarks: An html page with 500 div tags, with push stacks tags present:
So, we see an approximate 0.1ms difference between loading the page without push stacks enabled vs cached middleware. For me this is of course negligible. The auto-config could append the push stack middleware wrapped in Django's cached middlewares. An enhancement we could add later is to optionally handle minification in the middleware, which may bring better speed vs default! |
Beta Was this translation helpful? Give feedback.
-
I'm curious how "duplicate" is being defined and calculated. I'm assuming that I can push multiple times to the same stack, and so long as the pushes are different, they'll both work? |
Beta Was this translation helpful? Give feedback.
-
I love the idea! It would fit quite well into the way I'm using django-cotton currently. The name (bike-shedding...)The name "stack" suggests some kind of ordering and push/pop semantics. In this case, it seems to be more like an append-only list. Maybe the term "Portal" (coming from React) would also fit? Deduplication
Maybe the responsibility wrt deduplication could (and should?) be delegated to the user? For example an explicit "id" parameter for the Reducing the available component namespaceWhile two additional names (c-stack and c-push) seem fine, maybe we could just use
|
Beta Was this translation helpful? Give feedback.
-
A feature that I have long felt missing in cotton (and django) is the ability to push content from inside a component to anywhere else in the template. This is comparable to Laravel's 'push' 'stack' concept. To me, this particularly comes into its own when you have a component for example a Tooltip with its own 3rd party script or stylesheet, which may be used many times on one page and across multiple pages the frontend.
Attempts have been made to bring this to Django, for example django sekiszai, but there are limitations, tricky conditions and I think only works on the back of the blocks + extends pattern.
The problem
When we build out our component, in attempt to keep all effects encapsulated in the component, we may do this:
The current solution
Usually, any solution causes us to break component encapsulation and brings in the need to maintain dependencies somewhere else in the project.
push/stack pattern:
Features notes
<c-stack name="[stack name]" />
and<c-push to="[stack name]"></c-push>
How it works:
Further enhancements
stack
orpush
.Concerns
(I do have this as a working branch with tests)
<c- pattern
for components and also a few features like<c-slot
,<c-component
,<c-vars
. This is proposal will also add<c-stack
and<c-push
. Using more words in the component namespace prevents the developer from using them for component names. Do we consider this ok? I was thinking of an alternative syntax for the library-related terms like<c:slot
,<c:vars
etc.Voicing this for anyone interested and specifically want to kindly ask opinion from @mattbha and @copyfactory!
Beta Was this translation helpful? Give feedback.
All reactions