Django Classy Tags

4.1.0 · active · verified Wed Apr 15

django-classy-tags is a Python library that provides a class-based approach to writing custom template tags for Django. It aims to make template tag creation easier, shorter, and more fun by offering an extensible argument parser, significantly reducing boilerplate code while remaining fully compatible with the existing Django templating infrastructure. It is currently at version 4.1.0 and appears to have an active release cadence, with recent updates for Django and Python compatibility.

Warnings

Install

Imports

Quickstart

To create a simple tag, subclass `classytags.core.Tag` and implement the `render_tag` method, which receives the Django context and any defined tag arguments. Finally, register your tag class with a `django.template.Library` instance.

from classytags.core import Tag
from django import template

register = template.Library()

class HelloWorldTag(Tag):
    name = 'hello_world'

    def render_tag(self, context):
        return 'Hello, classy world!'

register.tag(HelloWorldTag)

# In your Django template (.html file):
# {% load my_app_tags %}
# {% hello_world %}
# Output: Hello, classy world!

view raw JSON →