Skip to main content

Class based template tags for Django

Project description

Please refer to the documentation in the docs/ directory for help. For a HTML rendered version of it please see here.

About this project

The goal of this project is to create a new way of writing Django template tags which is fully compatible with the current Django templating infrastructure. This new way should be easy, clean and require as little boilerplate code as possible while still staying as powerful as possible.

Features

  • Class based template tags.

  • Template tag argument parser.

  • Declarative way to define arguments.

  • Supports (theoretically infinite) parse-until blocks.

  • Extensible!

For the impatient

This is how a tag looks like using django-classy-tags:

from classytags.core import Tag, Options
from classytags.arguments import Argument
from django import template

register = template.Library()

class Hello(Tag):
    options = Options(
        Argument('name', required=False, default='world'),
        'as',
        Argument('varname', required=False, resolve=False)
    )

    def render_tag(self, context, name, varname):
        output = 'hello %s' % name
        if varname:
            context[varname] = output
            return ''
        return output

register.tag(Hello)

That’s your standard hello world example. Which can be used like this:

  • {% hello %}: Outputs hello world

  • {% hello "classytags" %}: Outputs hello classytags

  • {% hello as myvar %}: Outputs nothing but stores hello world into the template variable myvar.

  • {% hello "my friend" as othervar %}: Outputs nothing but stores hello my friend into the template variable othervar.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-classy-tags-0.4.tar.gz (17.1 kB view details)

Uploaded Source

File details

Details for the file django-classy-tags-0.4.tar.gz.

File metadata

File hashes

Hashes for django-classy-tags-0.4.tar.gz
Algorithm Hash digest
SHA256 6df0211e8c64d94673739bc880d0299c5c7d6c7659a62bcc406894891154a965
MD5 8cd002c9b6b5aacce84863130e83cab0
BLAKE2b-256 4ac4cfa8c1347a12f9cd3698b4783a35ff21e5e53c303f4295ef55b39220f97b

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page