Book Image

Django 1.0 Template Development

Book Image

Django 1.0 Template Development

Overview of this book

Table of Contents (17 chapters)
Django 1.0 Template Development
Credits
About the Author
About the Reviewers
Preface
Index

Writing your own template tags


Template tags require more steps to build because they are more flexible and can do more complex tasks than filters. Some of the biggest differences between tags and filters are that tags don't have to be attached to any values and they can take multiple arguments.

Writing a tag is a two-step process. First, you compile the tag text into a Node, which is a "chunk" of template that can be rendered. Second, you render the Node into output and return it to the template.

Creating another sample application

Let's leave the filtertest application alone and create a new application called tagtest to use as our sandbox for testing custom template tags.

In the /projects/customttags directory, run the startapp command:

$ python manage.py startapp tagtest

In customtags/settings.py, add our new tagtest application to the INSTALLED_APPS tuple. Add the highlighted line:

INSTALLED_APPS = (
    'customtags.filtertest',
    'customtags.tagtest',
)   

In customtags/urls.py, add the...