xzya / django-web-components Goto Github PK
View Code? Open in Web Editor NEWA simple way to create reusable template components in Django.
License: MIT License
A simple way to create reusable template components in Django.
License: MIT License
Hello, this package is great.
One issue encountered. It would be nice to be able to conditionally add slots:
{% card %}
{% if perms.can_edit %}
{% slot action %}<a href="/edit/">Edit</a>{% endslot %}
{% endif %}
…
{% endcard %}
However, the slot ends up being rendered regardless of the conditional block.
Hi. Thanks for this awesome library.
What do you think of easily adding class attributes to the component, those being available automatically in the template?
This could lead to very cool things like:
@component.register("link")
class Link(component.Component):
tag = "a"
template_name=".../link.html"
def get_context_data(self, **kwargs) -> dict:
context = super().get_context_data(**kwargs)
context.update(
{
"url": url = self.attributes.pop("url")
"htmx": url = self.attributes.pop("htmx", None)
}
)
return context
@component.register("linkbutton")
class LinkButton(Link):
tag = "button"
template_name=".../link.html"
{# link.html #}
<{{tag}}
{% if htmx %} hx-get="{{url}}"
{% else %}
{% if tag == "a" %} href={{ url }}{% endif %}
{% endif %}
>
{% render_slot slot.inner_block %}
</{{tag}}
In my code this leads to significant less duplication. But I have to write the tag into the get_context_data()
and make a super() call for inheriting. Class attributes would be inherited automatically.
The only thing that speaks agains that is to keep class attributes for "reactive" values later... ?
where to do I write the component registration? Is it inside views.py?
When will it support Django 5.0?
Hi Xzya, thanks for this wonderful piece of software. I have an idea, and would like to hear if you'd find this useful, I did not find a way of achieving it.
Would it be possible to modify the rendering for each block in dwc? In short, I'd like to have the inner_block
of a component render Markdown, e.g. using the markdownify
filter. I think this must somehow be done in the render_slot
method.
Would it be possible to add a customization there somehow?
I think this would be a helpful addon. It would enable component slots to be written in Markdown, uppercase etc.
Or am I overlooking something completely and it is easy to do?
This is really a wonderful package. I just love it. Makes my life so much easier.
I was wondering if you could agree with the following : it would be nice to have the ability to nest components and use their inner_block slots "normally". At the present moment, that generates a recursion error.
Here is an example
`component1 :
`component2 :
{% component1 %}
Some dummy content I would like to show
{% render_slot slots.inner_block %} {% endcomponent1 %}`Hi there! Thank you for this package.
I created a carousel component. Here is my component template
{% load components %}
<div id="{{ attributes.id }}" class="carousel">
<div class="carousel-container">
<div class="carousel-wrapper">
<!-- Slides -->
{% for slide in slots.slide %}
<div class="carousel-slide">{% render_slot slide %}</div>
{% endfor %}
</div>
</div>
</div>
When I use the component as in the snippet below it works correctly
{% carousel id="my-carousel" %}
{% slot slide %}Slide 1{% endslot %}
{% slot slide %}Slide 2{% endslot %}
{% slot slide %}Slide 3{% endslot %}
{% endcarousel %}
But if I use the slide slots inside the for tag (as below), nothing is printed
{% carousel id="my-carousel" %}
{% for item in items %}
{% slot slide %}{{ item.name }}{% endslot %}
{% endfor %}
{% endcarousel %}
I found that the content printed inside the for is in inner_block slot and the named slot "slide" is ignored.
Could you please suggest me how to use named slots inside the for tag?
Thank you for this package. It seems like an improvement over django-components.
It would be nice if we could mark certain attributes as mandatory in the component class. This would come in handy for people who are new to a certain component so they can easily see what attributes go in, without having to dig through the template.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.