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

Using include files


Though we saw the limitations of using include files earlier in this chapter, there are times when they can be useful. If you have a piece of content that you need to include in some child templates, it may not make sense to create extra parent/child relationships just to drop in this content. Let's look at two options that Django provides us to easily include content.

Using include

To take the contents of a template file and put it into another template, use the include tag. Pass the tag a template file name to include just like you did with the extends tag:

{% include "menu.html" %}

In this hypothetical example, the menu.html file will be loaded the same way other templates are loaded by the template engine. The location of the file is relative to the TEMPLATE_DIRS setting, and the file is parsed by the template engine using the same context variables as the template that called it.

Note

Because the include files are also rendered with the context, you can use template tags...