GithubHelp home page GithubHelp logo

xzya / django-web-components Goto Github PK

View Code? Open in Web Editor NEW
159.0 159.0 4.0 80 KB

A simple way to create reusable template components in Django.

License: MIT License

Python 99.95% HTML 0.05%
components django html

django-web-components's People

Contributors

xzya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

django-web-components's Issues

Conditional slots (slot in if)

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.

class attributes as context

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... ?

modify the slot render method, e.g. using markdownify

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?

Nesting components

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 :

{% render_slot slots.inner_block %}
`

`component2 :
{% component1 %}

Some dummy content I would like to show

{% render_slot slots.inner_block %} {% endcomponent1 %}`

How to use named slots inside the for tag?

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?

Mark attributes as mandatory

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.