Templates with Jinja2
The template is the Jinja2: the render_template (the 1, the context, the no the 1), the {% block %} (the extends, the base, the no the the), and the {{ }} (the variable, the escape, the no the XSS). The filter is the 20 (the date, the length, the truncate, the no the the), the include is the partial (the nav, the no the 1), and the custom is the filter (the app.template_filter, the no the the). This chapter covers the render_template (the 1, the no the 1), the extends (the block, the no the the), the filter (the 20, the no the the), and the custom filter (the no the the).
The render_template and the extends
<!-- the BASE (the 1, the no the
repeat, the no the 1) -->
<!-- templates/base.html -->
<!doctype html>
<html><head>
<title>{{ title }} | the blog</title>
</head><body>
{% include "nav.html" %}
<main>
{% block content %}{% endblock %}
</main>
</body></html>
<!-- the CHILD (the extends, the
block, the no the 1) -->
<!-- templates/index.html -->
{% extends "base.html" %}
{% block content %}
<h1>the post{{ posts|length }}</h1>
{% for post in posts %}
<article>
<h2>{{ post.title }}</h2>
<p>{{ post.body|truncate(80)
}}</p>
<time>{{ post.created_at
|datetimeformat("%d %b %Y") }}</time>
</article>
{% else %}
<p>the no the post</p>
{% endfor %}
{% endblock %}
The render_template is the 1: the 1 argument (the 1, the no the 1), and the context is the no (the 1, the no the 1, the no the the) - the {% extends %} is the 1 (the base, the block, the no the the), and the {% block %} is the no (the fill, the no the the, the no the 1) - the {{ }} is the escape (the XSS, the no, the |safe is the no the 1, the no the the), and the {% for %} is the loop (the {% else %} is the no, the no the 1, the no the the) - the 1 is the no (the 1, the no the 1, the no the the), and the filter is the next: the 20 the no the the.
The filter and the custom
<!-- the FILTER (the 20, the no
the the, the no the 1) -->
{{ post.body|truncate(80) }}
{{ post.body|length }}
{{ post.body|upper }}
{{ post.body|lower }}
{{ post.title|default("the no") }}
{{ post.created_at|datetimeformat(
"%d %b %Y") }}
{{ posts|length }}
{{ post.body|safe }}
<!-- the no: the no (the no,
the no, the no
the no, the no
the no) -->
<!-- the INCLUDE (the partial,
the no the 1, the no
the the) -->
{% include "nav.html" %}
<!-- the no: the no (the no,
the no, the no
the no, the no
the no) -->
<!-- the CUSTOM FILTER (the no
the the, the 1, the no
the 1) -->
<!-- the app.py -->
<!-- @app.template_filter("highlight") -->
<!-- def highlight(text, word): -->
<!-- return text.replace(word, -->
<!-- f"<mark>{word}</mark>") -->
{{ post.body|highlight("the")|safe }}
<!-- the no: the no (the no,
the no, the no
the no, the no
the no) -->
The filter is the 20: the truncate (the 80, the no the 1), the length (the count, the no the the), the upper the lower (the case, the no the 1), the default (the empty, the 1, the no the the), and the datetimeformat (the format, the no the 1) - the |safe is the no (the HTML, the trust, the no the 1, the no the the). The include is the partial: the nav.html is the 1 (the 20 the no the 1, the no the the), and the context is the no (the pass, the no the 1) - the custom filter is the 1: the @app.template_filter is the 1 (the function, the 2 the arg, the no the the), and the |safe is the no (the mark, the HTML, the no the 1) - the rule is the one: the render_template the 1, the extends the block, the filter the 20, and the custom the 1 - the next chapter's static is the file the CSS the JS the no the 1.
The Working Rules
- The render_template is the 1, the context the no. The 1 argument (the 1, the no the 1), and the context the no (the 1, the no the 1, the no the the) - the no the string (the render_template_string, the no the 1), and the no the 1 (the no the 1, the no the the).
- The extends is the 1, the block the no. The base.html the 1 (the nav, the block content, the no the the), and the child the block (the fill, the no the the, the no the 1) - the no the 1 (the no the 1, the no the the), and the no the 2 (the no the 1, the no the the).
- The {{ }} is the escape, the |safe the trust. The XSS the no (the no the 1), and the |safe the no (the HTML, the trust, the no the 1, the no the the) - the filter the 20 (the truncate, the length, the upper, the default, the datetimeformat), and the no the math (the view, the no the 1).
- The include is the partial, the custom the 1. The nav.html the 1 (the 20 the no the 1), and the context the no (the pass, the no the 1) - the @app.template_filter the 1 (the function, the 2 the arg, the no the the), and the |safe the no (the mark, the HTML, the no the 1).
Summary
The template is the Jinja2: the render_template the 1 (the 1, the context the no, the no the 1), the extends the 1 (the base, the block, the no the the, the child the fill), the {{ }} the escape (the XSS the no, the |safe the trust), and the {% for %} the loop (the {% else %} the no, the no the 1). The filter is the 20 (the truncate, the length, the upper the lower, the default, and the datetimeformat), the include is the partial (the nav.html, the context the no), and the custom filter is the 1 (the @app.template_filter, the 2 the arg, the |safe the no). The rule is the one: the render_template the 1, the extends the block, the filter the 20, and the custom the 1 - the next chapter's static is the file: the url_for("static") the 1, the static/ folder the no, and the no the Django (the no the 1, the no the the).