inPace
All coursesكل الدورات

Jinja templates: how inPace draws every page

  • about 4 h of lessonsحوالي 4 ساعة من الدروس
  • 4 modulesوحدات · 12 lessonsدروس
  • Englishالإنجليزية

A four-day course on Jinja2, taught through inPace's 50 templates: syntax, filters, autoescaping and Markup, inheritance with blocks, includes with context, macros, the Environment with custom globals and filters, the i18n extension, testing and security. Interview questions every day. Plan: 4 days, about 1.5 hours a day.

byمن إعداد inPace checkedموثّق

jinjapythontemplateswebinterview

On these roadmaps:ضمن هذه المسارات: Build inPace yourself

What's insideمحتوى الدورة

Each module ends with tasks. The next module opens when they're done.تنتهي كل وحدة بمهام، وتُفتح الوحدة التالية عند إنجازها.

  1. Day 1: Templates, variables, filters and escaping 3 lessonsدروس · 70 min د
    • Start here, and the three kinds of tags25 min د
    • Filters and tests20 min د
    • Autoescaping and Markup25 min د
    Practical taskمهمة عمليةExplain in your wordsاشرح بكلماتكQuizاختبار · 4 questionsأسئلة
  2. Day 2: Inheritance, includes and macros 3 lessonsدروس · 65 min د
    • One base, fifty pages25 min د
    • Includes with context15 min د
    • Macros: functions for markup25 min د
    Practical taskمهمة عمليةExplain in your wordsاشرح بكلماتكQuizاختبار · 4 questionsأسئلة
  3. Day 3: The Environment: globals, filters and translation 3 lessonsدروس · 65 min د
    • Setting up the Environment25 min د
    • Translation with the i18n extension25 min د
    • Custom filters and tests you write15 min د
    Practical taskمهمة عمليةExplain in your wordsاشرح بكلماتكQuizاختبار · 4 questionsأسئلة
  4. Day 4: Testing, performance, security, interview drill 3 lessonsدروس · 65 min د
    • Testing templates20 min د
    • Performance and security checklist20 min د
    • Interview drill25 min د
    Practical taskمهمة عمليةExplain in your wordsاشرح بكلماتكQuizاختبار · 4 questionsأسئلة

Read a lesson firstاقرأ درسًا أولًا

Start here, and the three kinds of tags

Your plan for these four days

Day Module Time
1 Templates, variables, filters and escaping about 1.5 h
2 Inheritance, includes and macros about 1.5 h
3 The Environment: globals, filters, i18n about 1.5 h
4 Testing, performance, security, interview drill about 1.5 h

Install Jinja (pip install jinja2) and keep inpace/templates/ and the top of inpace/app.py open. inPace has about 50 templates that all extend one base.html.

The syntax

Tag Does
{{ ... }} Prints an expression
{% ... %} A statement: if, for, set, block, macro...
{# ... #} A comment, never sent to the browser
<h1>{{ course.title }}</h1>
{% if progress.percent >= 100 %}
  <p class="done">Finished!</p>
{% elif progress.percent %}
  <progress value="{{ progress.percent }}" max="100"></progress>
{% else %}
  <p>Not started yet.</p>
{% endif %}

<ul>
{% for m in course.modules %}
  <li class="{{ 'open' if m.open else 'locked' }}">{{ loop.index }}. {{ m.title }}</li>
{% else %}
  <li>This course has no modules.</li>
{% endfor %}
</ul>
  • course.title tries the attribute, then course["title"]: dicts and objects look the same in templates.
  • loop.index, loop.first, loop.last, loop.length are available in every for.
  • for ... else runs when the list is empty.
  • A missing variable is Undefined: it prints as empty by default. StrictUndefined makes it an error (great in tests).

Rendering from Python

from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(loader=FileSystemLoader("templates"), autoescape=select_autoescape(["html"]))
html = env.get_template("lesson.html").render(course=c, progress=p)

Licenseالرخصة: CC-BY-4.0 · Updatedآخر تحديث 2026-11-13

Add to inPaceأضف إلى inPace