Jinja2のループと条件分岐
テンプレートでリストを表示し、マークアップを分岐させます
「Jinja2のループと条件分岐」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Markup That Reacts
Sometimes you must repeat rows or hide a section. Jinja2 adds logic tags so your template can loop and branch over data.
The Statement Delimiter
Logic uses {% %}, not double braces. Braces output a value, while percent tags run control flow like loops and conditions.
A Basic for Loop
Repeat markup with a for loop. Each pass binds the current item so you can print it inside the loop body.
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}Loops Must Close
Every block opens and closes. A for loop ends with endfor, which tells Jinja2 exactly where the repeated section stops.
The loop Variable
Inside a loop, Jinja2 gives you a special loop object. Use loop.index for a 1-based counter on each pass.
{% for u in users %}
{{ loop.index }}. {{ u }}
{% endfor %}Handling Empty Lists
A for block can include an else branch. It runs only when the list is empty, perfect for a no items message.
{% for t in todos %}{{ t }}{% else %}None yet{% endfor %}An if Condition
Show markup only when a test passes using if. It ends with endif and accepts ordinary Python-style comparisons.
{% if user %}
<p>Welcome back</p>
{% endif %}Adding else
Pair an if with else to handle the other case. One branch renders when true, the other when the test is false.
{% if user %}Hi{% else %}Please log in{% endif %}Multiple Branches with elif
Chain extra cases with elif. Jinja2 checks each in order and renders the first branch whose test is true.
{% if n > 0 %}+{% elif n < 0 %}-{% else %}0{% endif %}Combining Loops and Ifs
Nest an if inside a loop to filter as you go. This lets each item decide whether it appears in the output.
{% for p in posts %}{% if p.live %}{{ p.title }}{% endif %}{% endfor %}Keep Templates Readable
Light logic in templates is fine, but deep nesting hurts. Prepare filtered data in your view to keep markup clean.
Quick Check
Which delimiter does Jinja2 use for control flow like loops and conditionals?
Recap
You can now loop with for, branch with if and elif, use the loop counter, and handle empty lists, all inside templates. Well done! ✨
よくある質問
「Jinja2のループと条件分岐」レッスンは無料ですか?
はい。「Jinja2のループと条件分岐」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「Jinja2のループと条件分岐」で何を学びますか?
テンプレートでリストを表示し、マークアップを分岐させます ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「Jinja2のループと条件分岐」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- render_templateとtemplatesフォルダー
- テンプレートにデータを渡す
- Jinja2のループと条件分岐
- 自動エスケープとsafeフィルター