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

Django template backend #37

Merged
merged 1 commit into from
Aug 23, 2024
Merged

Django template backend #37

merged 1 commit into from
Aug 23, 2024

Conversation

pelme
Copy link
Owner

@pelme pelme commented Jul 25, 2024

This is an experimental template backend for Django that allows Django to load functions that return htpy content wherever you normally specify a template_name.

Put {"BACKEND": "htpy.django.HTPYTemplates", "NAME": "htpy"} in your TEMPLATES setting.

You could then use it something like this:

# views.py
from django.views.generic import ListView
from pizza.models import Pizza


class PizzaListView(ListView):
    model = Pizza
    template_name = "pizza.components.pizza_list"
# components.py
import htpy as h

def pizza_list(context, request):
    return h.ul[h.li[pizza for pizza in context['object_list']]]

Wherever you specify a template_name = "...", you can specify an import path to a function that returns htpy elements.

@pelme pelme force-pushed the django-template-loader branch 2 times, most recently from d75d4d9 to f89271d Compare July 25, 2024 18:31
@pelme pelme force-pushed the django-template-loader branch 4 times, most recently from be728e4 to 600069f Compare August 13, 2024 20:20
@pelme pelme changed the title Experimental Django template loader. Experimental Django template backend Aug 14, 2024
@Grauwolf
Copy link

Note: Django 5.1 requires template engines to implement a check() method.

https://docs.djangoproject.com/en/5.1/topics/checks/#field-checking

@pelme
Copy link
Owner Author

pelme commented Aug 20, 2024

Thanks for the heads up, will add a check() method too!

@pelme pelme changed the title Experimental Django template backend Django template backend Aug 23, 2024
@pelme pelme force-pushed the django-template-loader branch 2 times, most recently from 26187d0 to b23cd4f Compare August 23, 2024 20:04
@pelme pelme marked this pull request as ready for review August 23, 2024 20:13
@pelme pelme merged commit ae09e65 into main Aug 23, 2024
13 checks passed
@pelme
Copy link
Owner Author

pelme commented Aug 23, 2024

The Django template backend is now part of the 24.8.2 release! 🙂

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

Successfully merging this pull request may close these issues.

2 participants