Skip to content
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

Rendering components from views #18

Open
mixxorz opened this issue Aug 24, 2022 · 1 comment
Open

Rendering components from views #18

mixxorz opened this issue Aug 24, 2022 · 1 comment

Comments

@mixxorz
Copy link
Owner

mixxorz commented Aug 24, 2022

It may be useful to render components from views. I came across this while trying to use slippers components with HTMX.

@luanfonceca
Copy link

Hey @mixxorz, I was testing the same thing this morning as the stack I'm working on is django with htmx.
So what I found out is that it does work, even if you wrap the htmx "call" in a fragment you could use it as a parameter to another slipper component:

# inventory/views.py

class IOHistoryList(generic.ListView):
    model = IOHistory
    slug_field = 'code'
    template_name = 'ui/inventory/IOHistory.html' # <-- this is a slipper component

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.filter(
            product__code=self.kwargs.get('code')
        )
# ui/templates/IOHistory.html

<h3 class="text-md font-bold pb-4">IO History</h3>

<table class="table-auto">
  <tbody>
    {% for history in object_list %}
      <tr>
        <td>{{ history.date|date:'Y/m/d' }}</td>
        <td class="pl-5 text-green-60">
          {% if history.quantity_in %}
            <strong>↓{{ history.quantity_in }}</strong>
          {% else %}
            <strong>---</strong>
          {% endif %}
        </td>
        <td class="pl-3 text-red-60">
          {% if history.quantity_out %}
            <strong>↑{{ history.quantity_out }}</strong>
          {% else %}
            <strong>---</strong>
          {% endif %}
        </td>
      </tr>
    {% endfor %}
  </tbody>
</table>
# templates/inventory/product_detail.html
....
{% fragment as ioHistory %}
  <div hx-get="{% url 'inventory:io-history' product.code %}" hx-trigger="load"></div>
{% endfragment %}

{% Card header=cardHeader content=ioHistory %}

The page is loaded just fine
Screen Shot 2022-12-26 at 14 48 11

And also the "htmx" response from my slipper component
Screen Shot 2022-12-26 at 14 48 16

And I think that works just fine because the slipper component it's nothing different than a Django template, the only scenario where this may not work is if you're not rendering the template in your views, but would appreciate if you could include the error you faced bc I'm deciding to use slipper on a mid-size project here and would be great to know any possible issues in advance o/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants