Book Image

Learning Website Development with Django

Book Image

Learning Website Development with Django

Overview of this book

Table of Contents (18 chapters)
Learning Website Development with Django
Credits
About the Author
About the Reviewers
Preface
Index

Custom Template Tags and Filters


The Django template system comes with many template tags and filters that make writing templates an easy and flexible job. Sometimes however, you may wish to extend the template system with your own tags and filters. This usually happens when you find yourself repeating the same tag structure many times, and you wish to wrap the structure into a single tag. Or maybe there is a filter that you want to add to the template system. The pagination system that we wrote in Chapter 9 is a good example of this. Each time we wanted to include the paginator in a page, we had to use the same structure of template tags. It would be cleaner and easier if we could wrap the paginator into a single template tag.

Guess what? Django already allows you to do so, and it is quite easy too! You basically add a new package to your application called templatetags, and put modules that contain tags and filters in it. Let's learn about this by adding a filter that capitalizes a string...