الفرق بين المراجعتين ل"Liquid/control-flow"

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
(أنشأ الصفحة ب'== الوسوم Tags ذات النوع Control flow == تنشِئ وسوم Control flow الشروط التي تحدّد تنفيذ كتل شيفرة Liquid أم لا....')
(لا فرق)

مراجعة 00:47، 2 أغسطس 2021

الوسوم Tags ذات النوع Control flow

تنشِئ وسوم Control flow الشروط التي تحدّد تنفيذ كتل شيفرة Liquid أم لا.

if

تنفّذ كتلة من الشيفرة إذا تحقق شرط معين فقط أي إذا كانت قيمته true.

الدخل
{% if product.title == "Awesome Shoes" %}
  These shoes are awesome!
{% endif %}
الخرج
These shoes are awesome!‎

unless

وهو عكس وسم if، إذ ينفّذ كتلة من الشيفرة إذا لم يتحقّق شرط معين.

الدخل
{% unless product.title == "Awesome Shoes" %}
  These shoes are not awesome.
{% endunless %}
الخرج
These shoes are not awesome.‎

وسيكون ذلك مكافئًا لتنفيذ بما يلي:

{% if product.title != "Awesome Shoes" %}
  These shoes are not awesome.
{% endif %}

elsif / else

يضيف مزيدًا من الشروط ضمن كتلة if أو unless.

الدخل
<!-- If customer.name = "anonymous" -->
{% if customer.name == "kevin" %}
  Hey Kevin!
{% elsif customer.name == "anonymous" %}
  Hey Anonymous!
{% else %}
  Hi Stranger!
{% endif %}
الخرج
Hey Anonymous!‎

case / when

ينشئان عبارات تحويلية لتنفيذ كتلة معينة من الشيفرة عندما يكون للمتغيّر قيمة محددة، إذ يهيّئ الوسم case العبارة التحويلية، وتحدّد عبارات when الشروط المختلفة.

توفّر عبارة else الاختيارية في نهاية case شيفرةً للتنفيذ إن لم يتحقّق أيّ من الشروط.

الدخل
{% assign handle = "cake" %}
{% case handle %}
  {% when "cake" %}
     This is a cake
  {% when "cookie", "biscuit" %}
     This is a cookie
  {% else %}
     This is not a cake nor a cookie
{% endcase %}
الخرج
This is a cake

مصادر