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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
(أنشأ الصفحة ب'== الوسوم Tags ذات النوع Control flow == تنشِئ وسوم Control flow الشروط التي تحدّد تنفيذ كتل شيفرة Liquid أم لا....')
 
سطر 3: سطر 3:
  
 
=== if ===
 
=== if ===
تنفّذ كتلة من الشيفرة إذا تحقق شرط معين فقط أي إذا كانت قيمته <code>true</code>.
+
ينفّذ هذا الوسم كتلة من الشيفرة إذا تحقق شرط معين فقط، أي إذا كانت قيمته <code>true</code>.
 
{| class="wikitable"
 
{| class="wikitable"
 
|+
 
|+
 
!الدخل
 
!الدخل
 +
!الخرج
 
|-
 
|-
 
|<syntaxhighlight lang="liquid">
 
|<syntaxhighlight lang="liquid">
سطر 12: سطر 13:
 
   These shoes are awesome!
 
   These shoes are awesome!
 
{% endif %}
 
{% endif %}
 +
</syntaxhighlight>
 +
|<syntaxhighlight lang="liquid">
 +
These shoes are awesome!
 
</syntaxhighlight>
 
</syntaxhighlight>
 
|}
 
|}
{| class="wikitable"
 
!الخرج
 
|-
 
|<code>These shoes are awesome!‎</code>
 
|}
 
 
 
=== unless ===
 
=== unless ===
وهو عكس وسم <code>if</code>، إذ ينفّذ كتلة من الشيفرة إذا لم يتحقّق شرط معين.
+
وهو عكس الوسم <code>if</code>، إذ ينفّذ كتلة من الشيفرة إذا لم يتحقّق شرط معين.
 
{| class="wikitable"
 
{| class="wikitable"
 
!الدخل
 
!الدخل
 +
!الخرج
 
|-
 
|-
 
|<syntaxhighlight lang="liquid">
 
|<syntaxhighlight lang="liquid">
سطر 29: سطر 28:
 
   These shoes are not awesome.
 
   These shoes are not awesome.
 
{% endunless %}
 
{% endunless %}
 +
</syntaxhighlight>
 +
|<syntaxhighlight lang="liquid">
 +
These shoes are not awesome.
 
</syntaxhighlight>
 
</syntaxhighlight>
 
|}
 
|}
{| class="wikitable"
+
وسيكون ذلك مكافئًا لتنفيذ ما يلي:<syntaxhighlight lang="liquid">
!الخرج
 
|-
 
|<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.
سطر 43: سطر 40:
  
 
=== elsif / else ===
 
=== elsif / else ===
يضيف مزيدًا من الشروط ضمن كتلة <code>if</code> أو <code>unless</code>.
+
يضيف هذا الوسم مزيدًا من الشروط ضمن كتلة <code>if</code> أو <code>unless</code>.
 
{| class="wikitable"
 
{| class="wikitable"
 
!الدخل
 
!الدخل
 +
!الخرج
 
|-
 
|-
 
|<syntaxhighlight lang="liquid">
 
|<syntaxhighlight lang="liquid">
سطر 56: سطر 54:
 
   Hi Stranger!
 
   Hi Stranger!
 
{% endif %}
 
{% endif %}
 +
</syntaxhighlight>
 +
|<syntaxhighlight lang="liquid">
 +
Hey Anonymous!
 
</syntaxhighlight>
 
</syntaxhighlight>
 
|}
 
|}
{| class="wikitable"
 
!الخرج
 
|-
 
|<code>Hey Anonymous!‎</code>
 
|}
 
 
 
=== case / when ===
 
=== case / when ===
ينشئان عبارات تحويلية لتنفيذ كتلة معينة من الشيفرة عندما يكون للمتغيّر قيمة محددة، إذ يهيّئ الوسم <code>case</code> العبارة التحويلية، وتحدّد عبارات <code>when</code> الشروط المختلفة.
+
ينشئ هذا الوسم عبارات تحويلية switch statement لتنفيذ كتلة معينة من الشيفرة عندما يكون للمتغيّر قيمة محددة، إذ يهيّئ الوسم <code>case</code> العبارة التحويلية، وتحدّد عبارات <code>when</code> الشروط المختلفة.
  
 
توفّر عبارة <code>else</code> الاختيارية في نهاية <code>case</code> شيفرةً للتنفيذ إن لم يتحقّق أيّ من الشروط.
 
توفّر عبارة <code>else</code> الاختيارية في نهاية <code>case</code> شيفرةً للتنفيذ إن لم يتحقّق أيّ من الشروط.
 
{| class="wikitable"
 
{| class="wikitable"
 
!الدخل
 
!الدخل
 +
!الخرج
 
|-
 
|-
 
|<syntaxhighlight lang="liquid">
 
|<syntaxhighlight lang="liquid">
سطر 81: سطر 77:
 
     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 الرسمي.]

مراجعة 15:30، 4 أغسطس 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

ينشئ هذا الوسم عبارات تحويلية 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

مصادر