liveBook · Manning

Templating language

As explained in Django in Action

A template language is a language embedded in templates for replacing values, accessing data, and controlling rendered output.

A templating language embeds small instructions in text so rendered output can use supplied data and make limited display decisions. In Django, a Template holds the template text and a Context supplies the data used to render it. Although templates commonly produce HTML, the engine can render any text, including email or prefilled form text. section 3.1 section 1.1

Variables use matched double braces: {{ name }} looks up name in the context and inserts its value. Dot notation reaches object attributes, dictionary values, list or tuple items, and no-argument callables. Thus {{ person.name }} can read either an attribute or dictionary key, while {{ fruit.2 }} selects a list item. section 3.2 section 3.4

The language has three main kinds of instruction:

  • Variables replace a placeholder with context data, such as {{ name }}. section 3.2
  • Tags perform rendering actions. {% now "Y" %} renders the current year; paired tags such as {% if %} and {% endif %} conditionally render their enclosed block. section 3.2
  • Filters modify a variable value with a pipe, such as {{ name | upper }}, which produces uppercase output. section 3.2

Render a greeting

Create Template("Hello {{ name }}"), create a Context containing a name value, then call render(context). The result is an ordinary output string such as Hello Rumpelstiltskin. This small loop—template plus data, then rendering—is the basic model behind a generated page. section 3.4

Tags can control repeated or conditional page content rather than merely insert values. A song-by-year view can load an inheriting page template and use a looping tag to turn song results into rows of an HTML table. After rendering, Django packages the HTML in an HttpResponse for the browser. section 1.1

Template syntax resembles Python in places, but it is deliberately its own language. For example, template dot notation handles dictionary and list access where Python would often use square brackets. Django provides more than 80 built-in tags and filters and allows the language to be extended. section 3.4 section 3.2

Listing 3.1 Rendering a template in the Django REPL
>>> from django.template import Template, Context
>>> t = Template("Hello {{name}}")  #1
>>> c = Context({"name":"Rumpelstiltskin"}) #2
>>> t.render(c)  #3
'Hello Rumpelstiltskin'

Use the REPL transcript to trace the complete rendering sequence: import Template and Context, provide a name, and call render(). section 3.4

How Django in Action covers it

How deep it goes
Explains variable lookup, dot notation, tags, filters, and direct Template.render() use, while deferring detailed tag coverage.
Examples
Demonstrates a rendered greeting, lorem ipsum tags, conditional output, and song rows generated in an HTML table.
What you can do after
Apply it via Django Template and Context objects to render data-driven text and control displayed content with tags and filters.
How it connects
Connects Template and Context with template inheritance, views, QuerySet results, HttpResponse objects, and the ORM.

Also explained in

Compare how 3 books explain templating language →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.