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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
(أنشأ الصفحة ب'== الوسوم Tags ذات النوع Control flow == تنشِئ وسوم Control flow الشروط التي تحدّد تنفيذ كتل شيفرة Liquid أم لا....')
 
ط (مراجعة)
 
(مراجعة متوسطة واحدة بواسطة مستخدم واحد آخر غير معروضة)
سطر 1: سطر 1:
== الوسوم Tags ذات النوع Control flow ==
+
<noinclude>{{DISPLAYTITLE:وسوم التحكم في تدفق وسير التنفيذ في Liquid}}</noinclude>
تنشِئ وسوم Control flow الشروط التي تحدّد تنفيذ كتل شيفرة Liquid أم لا.
+
تنشِئ وسوم التحكم في تدفق وسير التنفيذ Control flow tags الشروط التي تحدّد تنفيذ كتل شيفرة Liquid أم لا وتتحكم في تدفق سير تنفيذ الشيفرة.
  
=== if ===
+
= الشرط if =
تنفّذ كتلة من الشيفرة إذا تحقق شرط معين فقط أي إذا كانت قيمته <code>true</code>.
+
ينفّذ هذا الوسم كتلة من الشيفرة إذا تحقق شرط معين فقط، أي إذا كانت قيمته <code>true</code>.<syntaxhighlight lang="liquid">
{| class="wikitable"
 
|+
 
!الدخل
 
|-
 
|<syntaxhighlight lang="liquid">
 
 
{% if product.title == "Awesome Shoes" %}
 
{% if product.title == "Awesome Shoes" %}
 
   These shoes are awesome!
 
   These shoes are awesome!
 
{% endif %}
 
{% endif %}
 +
</syntaxhighlight>الخرج:<syntaxhighlight lang="liquid">
 +
These shoes are awesome!
 
</syntaxhighlight>
 
</syntaxhighlight>
|}
+
== عدم التحقق unless ==
{| class="wikitable"
+
وهو عكس الوسم <code>if</code>، إذ ينفّذ كتلة من الشيفرة إذا لم يتحقّق شرط معين.<syntaxhighlight lang="liquid">
!الخرج
 
|-
 
|<code>These shoes are awesome!‎</code>
 
|}
 
 
 
=== unless ===
 
وهو عكس وسم <code>if</code>، إذ ينفّذ كتلة من الشيفرة إذا لم يتحقّق شرط معين.
 
{| class="wikitable"
 
!الدخل
 
|-
 
|<syntaxhighlight lang="liquid">
 
 
{% unless product.title == "Awesome Shoes" %}
 
{% unless product.title == "Awesome Shoes" %}
 
   These shoes are not awesome.
 
   These shoes are not awesome.
 
{% endunless %}
 
{% endunless %}
 +
</syntaxhighlight>الخرج:<syntaxhighlight lang="liquid">
 +
These shoes are not awesome.
 
</syntaxhighlight>
 
</syntaxhighlight>
|}
+
وسيكون ذلك مكافئًا لتنفيذ ما يلي:<syntaxhighlight lang="liquid">
{| class="wikitable"
 
!الخرج
 
|-
 
|<code>These shoes are not awesome.‎</code>
 
|}
 
وسيكون ذلك مكافئًا لتنفيذ بما يلي:<syntaxhighlight lang="liquid">
 
 
{% if product.title != "Awesome Shoes" %}
 
{% if product.title != "Awesome Shoes" %}
 
   These shoes are not awesome.
 
   These shoes are not awesome.
سطر 42: سطر 24:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== elsif / else ===
+
== الشرط المتعدد elsif / else ==
يضيف مزيدًا من الشروط ضمن كتلة <code>if</code> أو <code>unless</code>.
+
يضيف هذا الوسم مزيدًا من الشروط ضمن كتلة <code>if</code> أو <code>unless</code>.<syntaxhighlight lang="liquid">
{| class="wikitable"
 
!الدخل
 
|-
 
|<syntaxhighlight lang="liquid">
 
 
<!-- If customer.name = "anonymous" -->
 
<!-- If customer.name = "anonymous" -->
 
{% if customer.name == "kevin" %}
 
{% if customer.name == "kevin" %}
سطر 56: سطر 34:
 
   Hi Stranger!
 
   Hi Stranger!
 
{% endif %}
 
{% endif %}
 +
</syntaxhighlight>الخرج:<syntaxhighlight lang="liquid">
 +
Hey Anonymous!
 
</syntaxhighlight>
 
</syntaxhighlight>
|}
+
== الشرط التبديلي case / when ==
{| class="wikitable"
+
ينشئ هذا الوسم عبارات تحويلية switch statement لتنفيذ كتلة معينة من الشيفرة عندما يكون للمتغيّر قيمة محددة، إذ يهيّئ الوسم <code>case</code> العبارة التحويلية، وتحدّد عبارات <code>when</code> الشروط المختلفة.
!الخرج
 
|-
 
|<code>Hey Anonymous!‎</code>
 
|}
 
  
=== case / when ===
+
توفّر عبارة <code>else</code> الاختيارية في نهاية <code>case</code> شيفرةً للتنفيذ إن لم يتحقّق أيّ من الشروط.<syntaxhighlight lang="liquid">
ينشئان عبارات تحويلية لتنفيذ كتلة معينة من الشيفرة عندما يكون للمتغيّر قيمة محددة، إذ يهيّئ الوسم <code>case</code> العبارة التحويلية، وتحدّد عبارات <code>when</code> الشروط المختلفة.
 
 
 
توفّر عبارة <code>else</code> الاختيارية في نهاية <code>case</code> شيفرةً للتنفيذ إن لم يتحقّق أيّ من الشروط.
 
{| class="wikitable"
 
!الدخل
 
|-
 
|<syntaxhighlight lang="liquid">
 
 
{% assign handle = "cake" %}
 
{% assign handle = "cake" %}
 
{% case handle %}
 
{% case handle %}
سطر 81: سطر 50:
 
     This is not a cake nor a cookie
 
     This is not a cake nor a cookie
 
{% endcase %}
 
{% endcase %}
 +
</syntaxhighlight>الخرج:<syntaxhighlight lang="liquid">
 +
This is a cake
 
</syntaxhighlight>
 
</syntaxhighlight>
|}
 
{| class="wikitable"
 
!الخرج
 
|-
 
|<code>This is a cake</code>
 
|}
 
 
 
== مصادر ==
 
== مصادر ==
  
 
* [https://shopify.github.io/liquid/tags/control-flow/ صفحة Control flow في توثيق Liquid الرسمي.]
 
* [https://shopify.github.io/liquid/tags/control-flow/ صفحة Control flow في توثيق Liquid الرسمي.]
 +
[[تصنيف:Liquid]]

المراجعة الحالية بتاريخ 11:51، 5 أغسطس 2021

تنشِئ وسوم التحكم في تدفق وسير التنفيذ Control flow tags الشروط التي تحدّد تنفيذ كتل شيفرة 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

ينشئ هذا الوسم عبارات تحويلية switch statement لتنفيذ كتلة معينة من الشيفرة عندما يكون للمتغيّر قيمة محددة، إذ يهيّئ الوسم 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

مصادر