0Pricing
Flask Academy · درس

الحلقات والشروط في Jinja2

صيّر القوائم وفرّع عناصر الترميز في القوالب.

الحلقات والشروط في Jinja2 درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «الحلقات والشروط في Jinja2»؟

صيّر القوائم وفرّع عناصر الترميز في القوالب. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «الحلقات والشروط في Jinja2»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. render_template ومجلد templates
  2. تمرير البيانات إلى قالب
  3. الحلقات والشروط في Jinja2
  4. التحويل التلقائي ومرشّح safe
← العودة إلى Flask Academy