樣板

樣板標籤會告知 Liquid 在何處停用註解或非 Liquid 標記的處理,以及如何在樣板檔案之間建立關聯。

comment

允許您在 Liquid 樣板中留下未渲染的程式碼。 開頭和結尾 comment 區塊中的任何文字都不會被印出,並且其中的任何 Liquid 程式碼都不會被執行。

輸入

{% assign verb = "turned" %}
{% comment %}
{% assign verb = "converted" %}
{% endcomment %}
Anything you put between {% comment %} and {% endcomment %} tags
is {{ verb }} into a comment.

輸出



Anything you put between  tags
is turned into a comment.

行內註解

您可以使用行內註解來防止運算式被渲染或輸出。標籤內的任何文字也不會被渲染或輸出。

您可以建立多行行內註解。但是,每一行都必須以 # 開頭。

輸入

{% # for i in (1..3) -%}
  {{ i }}
{% # endfor %}

{%
  ###############################
  # This is a comment
  # across multiple lines
  ###############################
%}

輸出



liquid 標籤內的行內註解

您可以在 liquid 標籤內使用行內註解標籤。您想要註解的每一行都必須使用該標籤。

輸入

{% liquid
  # this is a comment
  assign topic = 'Learning about comments!'
  echo topic
%}

輸出

Learning about comments!

raw

暫時停用標籤處理。這對於產生使用衝突語法的特定內容(例如 MustacheHandlebars)很有用。

輸入

{% raw %}
In Handlebars, {{ this }} will be HTML-escaped, but {{{ that }}} will not.
{% endraw %}

輸出


In Handlebars, {{ this }} will be HTML-escaped, but {{{ that }}} will not.

liquid

將多個標籤包含在一組分隔符號中,以便更簡潔地編寫 Liquid 邏輯。

{% liquid
case section.blocks.size
when 1
  assign column_size = ''
when 2
  assign column_size = 'one-half'
when 3
  assign column_size = 'one-third'
else
  assign column_size = 'one-quarter'
endcase %}

因為在 liquid 標籤內開啟的任何標籤區塊也必須在同一個標籤內關閉,所以請使用 echo 來輸出資料。

echo

在渲染的 HTML 中輸出一個運算式。這與將運算式包裝在 {{}} 中相同,但在 liquid 標籤內運作,並支援篩選器

輸入

{% liquid
for product in collection.products
  echo product.title | capitalize
endfor %}

輸出

Hat Shirt Pants

render

在目前的樣板中插入另一個樣板的渲染內容。

{% render "template-name" %}

請注意,您不需要寫入檔案的 .liquid 副檔名。

渲染的樣板中的程式碼無法自動存取使用父樣板中的 變數標籤 所指派的變數。同樣地,在渲染的樣板中指派的變數也無法被任何其他樣板中的程式碼存取。

render (參數)

可以使用 變數標籤 指派的變數,方法是在 render 標籤上將它們列為參數來傳遞給樣板。

{% assign my_variable = "apples" %}
{% render "name", my_variable: my_variable, my_other_variable: "oranges" %}

可以將一個或多個物件傳遞給樣板。

{% assign featured_product = all_products["product_handle"] %}
{% render "product", product: featured_product %}

with

可以使用 with 和可選的 as 參數,將單一物件傳遞給樣板。

{% assign featured_product = all_products["product_handle"] %}
{% render "product" with featured_product as product %}

在上面的範例中,渲染的樣板中的 product 變數將保存父樣板中 featured_product 的值。

for

可以使用 for 和可選的 as 參數,針對可列舉物件的每個值渲染一次樣板。

{% assign variants = product.variants %}
{% render "product_variant" for variants as variant %}

在上面的範例中,樣板將針對產品的每個變體渲染一次,並且 variant 變數將在每次迭代中保存不同的產品變體物件。

當使用 for 參數時,可以在渲染的樣板中存取 forloop 物件。

include

include 標籤已棄用;請改用 render

在目前的樣板中插入另一個樣板的渲染內容。

{% include "template-name" %}

include 標籤的工作方式與 render 標籤類似,但它允許渲染的樣板內的程式碼存取和覆寫其父樣板中的變數。它已被棄用,因為它處理變數的方式會降低效能,並使 Liquid 程式碼更難以閱讀和維護。

請注意,當使用 render 標籤渲染樣板時,無法在樣板內使用 include 標籤。