In addition to extending templates with parent templates, Django allows you to load the entire contents of a template into an existing template using the include tag. Unlike the extends tag, the include tag simply inserts the full contents of the specified template file into the existing template.
The include tag can be useful for creating "canned" components for your website, such as a calendar, that need to be displayed on several web pages.
For example, the following line of code embeds a template named comments.html into a template:
{% include "comments.html" %}
Did you Know?
You can specify a variable name instead of a filename in the include tag. This allows you to dynamically control what the content will be by specifying different template filenames from within the view code.
By the Way
Variables that are available in the view template also are available in templates that are embedded using the include tag.
Try It Yourself: Embed a Template File in Another TemplateIn this section, you will create a quote.html template that can be embedded into other template files. Follow these steps to create a template called quote.html and embed it into person_details.html:
Listing 7.12. Full Contents of iFriends/templates/person_details.html
|