Django Templates - Ambiguous-Squids/Bangazon_initial_site GitHub Wiki
-
A template is simply a text file. It can generate any text-based format (HTML, XML, CSV, etc.).
-
A template contains variables, which get replaced with values when the template is evaluated, and tags, which control the logic of the template.
{% block title %}{{ section.title }}{% endblock %}
{% block content %}
<h1>{{ section.title }}</h1>
{% for story in story_list %}
<h2>
<a href="{{ story.get_absolute_url }}">
{{ story.headline|upper }}
</a>
</h2>
<p>{{ story.tease|truncatewords:"100" }}</p>
{% endfor %}
{% endblock %}
- Variables look like this: {{ variable }}.
- When the template engine encounters a variable, it evaluates that variable and replaces it with the result. * Variable names consist of any combination of alphanumeric characters and the underscore ("_").
- The dot (".") also appears in variable sections, although that has a special meaning. Use a dot (.) to access attributes of a variable.
- Importantly, you cannot have spaces or punctuation characters in variable names.
-
You can modify variables for display by using filters.
-
Filters look like this: {{ name|lower }}. This displays the value of the {{ name }} variable after being filtered through the lower filter, which converts text to lowercase.
-
Use a pipe (|) to apply a filter.
-
Filters can be “chained.” The output of one filter is applied to the next. {{ text|escape|linebreaks }} is a common idiom for escaping text contents.
-
Some filters take arguments. A filter argument looks like this: {{ bio|truncatewords:30 }}. This will display the first 30 words of the bio variable.
-
Filter arguments that contain spaces must be quoted; for example, to join a list with commas and spaces you’d use {{ list|join:", " }}.
-
Django provides about sixty built-in template filters.