移至主內容
首頁  >  Drupal目錄  >  欄位值在Twig模板引擎的應用

欄位值在Twig模板引擎的應用

Tag :
Twig, Template, Field, MachineName
Written by Shiuan on 12 October 2023

簡而言之,如何使用本指南:
Manage Display 管理界面可以更改欄位標籤和內容的顯示方式。以下當本文想展示如何輸出僅管理界面的內容時,我們使用「display」。當本文談到內容的原始值時,使用「value」。
本文已使用 "field_name" 作為欄位的機器名稱。您可以在內容類型的「Manage Fields」中找到機器名稱,並替代"field_name" 。

目錄

  1. Body
  2. Text
  3. List
  4. Link
  5. Image
  6. File
  7. Boolean
  8. Number
  9. Email
  10. Phone
  11. Date
  12. Taxonomy/Entity Reference
  13. Labels for any field type
  14. Multi-value for any field type
  15. Examples
  16. Concepts/Background

1. Body

Content type認為body field是一個基本的Text (formatted, long, with summary)欄位類型。

Content (display): {{ content.body.0 }} 這將顯示在Manage Display中設置的內容。如果body field 設為 Summary 或 Trimmed,則會顯示摘要內容。

Content (value, formatted): {{ node.body.value|raw }} 這將顯示文本自身的內容,不管在Manage Display中設置了什麼。這將解析任何HTML。

Note據了解,Twig過濾器raw可能存在安全問題,但尚未找到另一種方法來執行此操作。

Content (value, unparsed HTML): {{ node.body.0.value }}顯示欄位的原始值,包括未解析的HTML。

Content (value, plain text): {{ node.body.0.value|striptags }} 與上述相同,但將刪除HTML,就像它是純文本一樣。

Summary: {{ node.body.summary }}


2.Text

與body非常相似

Text (plain) and Text (plain, long)

Content (display): {{ content.field_name }}

Content (value): {{content.field_name.0 }}

Text (formatted), Text (formatted, long), Text (formatted, long, with summary)

Content (display): {{ content.field_name.0 }} 再次顯示在Manage Display中設置的內容,可能是 Default Trimmed。

Content (value, formatted): {{ node.field_name.0.value|raw }}

Content (value, unparsed): {{ node.field_name.0.value }}顯示欄位的原始值,包括未解析的HTML。

Content (value, plain text): {{ node.field_name.0.value|striptags }}

Summary: {{ node.field_name.summary }}


3.List

List (float), List (integer), List (text)

這些是具有 key 和 label 的欄位。key 是儲存的值。請記住,這裡的 label 不是指欄位標籤,而是 key 的 label。

Content (display): {{ content.field_name.0 }}如果顯示設置為 Key,則將顯示 key。如果設置為 Label,則將顯示 label。

Content (label):本文尚未找到如何以與提取 key 相同的方式提取 label。因此,您可通過更改Manage Display為Label來執行此操作。

Content (key): {{ node.field_name.0.value }}

這將顯示欄位中的第一個項目。對於多數值欄位,您可以通過將0更改為其他數字來顯示不同的項目。請記住從0開始計數;第二個項目將是1。

要顯示所有項目,請參閱13章。


4.Link

Content (display): {{ content.field_name }}

如果這是個多數值的欄位,上面的示例將輸出所有的值,正如在“Manage Display”中的配置。若要以這種方式提取僅一個值,請添加索引,如下所示: {{ content.field_name.0 }}

Link only: {{ content.field_name.0.url }}

Link text only: {{ content.field_name.0.title }}

這將顯示欄位中的第一個項目。對於多數值欄位,可以通過將0更改為其他數字來顯示不同的項目。請記住從0開始計數,第二個項目將是1。


5.Image

Content (display — the image): {{ content.field_name.0 }}

Content (image path): {{ file_url(content.field_name['#items'].entity.uri.value) }}

Alt text: {{ node.field_name.alt }}

Title text: {{ node.field_name.title }}

Height/Width:

{{ node.field_name.height }} or {{ node.field_name.width }}


6.File

Content (display): {{ content.field_name.0 }}

Content (file path): {{ file_url(content.field_name['#items'].entity.uri.value) }}

Description: {{ node.field_name.description }}


7.Boolean

這個部分和 List 非常相似,但 key 的運作方式略有不同。對於boolean的欄位, key 只有兩個選項,0(未選擇)或1(已選擇)。

Content (display): {{ content.field_name.0 }}

Content (key): {{ node.field_name.0.value }}

若要顯示所有的項目,請參閱14。


8.Number

Number (decimal) and Number (float)

Content (display): {{ content.field_name.0 }}

Content (value): {{ node.field_name.0.value }} This would remove any prefix/suffix.

若要顯示所有的項目,請參閱14。


9.Email

Content (display): {{ content.field_name.0 }}

Content (value): {{ node.field_name.0.value }} This shows the plain text email.

若要顯示所有的項目,請參閱14。


10.Phone

Content (display): {{ content.field_name.0 }}

Content (value): {{ node.field_name.0.value }}

若要顯示所有的項目,請參閱14。


11.Date

Content (display): {{ content.field_name.0 }}

Content (value): {{ node.field_name.0.value }}

這將輸出通常類似於以下的日期信息:週一,2019年03月25日–02:57。

若要顯示所有的項目,請參閱14。


12.Taxonomy/Entity Reference

本文對這個欄位的了解不夠深入,但關於「分類術語參考」(Taxonomy term reference)的部分,本文可以提供一些資訊。

Content (display): {{ content.field_name.0 }}

Content (value): {{ node.field_name.0.target_id }}

這顯示了 tag id,它是一個數字。

Content (term title): {{ content.field_name[0]['#title'] }}

Content (term path): {{ content.field_name[0]['#url'] }}


13.Labels for any field type

{{ node.field_name.fieldDefinition.label }}


14.Multi-value for any field type

當一個欄位是多數值欄位時,以下是如何在 node template 中以多種 HTML 控制輸出。

在這裡,{{ item }} 會按照在 Manage Display 中的配置,顯示完整的欄位內容。以下是如何在 <ul> 中使用它的方式。

{% if content.field_name[0] %}
  <ul>
    {% for key, item in content.field_name if key|first != '#' %}
      <li>{{ item }}</li>
    {% endfor %}
  </ul>
{% endif %}

您可以將這種多數值的技巧與上述的技巧結合使用。


15.Examples

範例1:製作一個 ul,其中包含 List 欄位的 keys。

{% if content.field_name[0] %}
  <ul>
    {% for key, item in node.field_name if key|first != '#' %}
      <li>{{ item.value }}</li>
    {% endfor %}
  </ul>
{% endif %}

  1. 要獲得整個欄位,使用{{ content.field_name }}, 如果想要檢查是否至少有一個值,可以使用 {% if content.field_name[0] %}
  2. 要獲得單個 key,我會使用{{ node.field_name.0.value }}, 並使用 node.field_name 來提取項目。

範例2:在一個 <ul> 中獲取標籤的名稱。

{% if content.field_name[0] %}
  <ul>
    {% for key, item in content.field_name if key|first != '#' %}
      <li>{{ item['#title'] }}</li>
    {% endfor %}
  </ul>
{% endif %}

  1. 要獲得整個欄位,使用{{ content.field_name }}, 如果想要檢查是否存在一個值,可以使用 {% content.field_name[0] %}
  2. 要獲得單一 tag title,使用{{ content.field_name[0]['#title'] }}, 所以在段落可使用欄位的第一部分:content.field_name。
  3. 數字 0 代表 list 中的某個項目,所以我可以用 "item" 代替它以及它之前的所有內容,然後尋找 ['#title']。

16.Concepts/Background

請留意上面提到的 content 和 node 的使用情況。在類似 node--page.html.twig 的 node template中,content variable 會查看在該內容類型的 Manage Display 中所選擇的選項。本文盡可能地在使用者界面中進行操作,但有時我們需要在這些設置之前獲取欄位的部分內容

這就是當我們進入 node 以獲取我們想要的內容時的情況。更多 raw 信息儲存在 Display 之上或之前


以上就是欄位值在Twig模板引擎的應用

返回Drupal
首頁  >  Drupal  >  欄位值在Twig模板引擎的應用

欄位值在Twig模板引擎的應用

Tag :
Twig, Template, Field, MachineName
Written by Shiuan on 12 October 2023

簡而言之,如何使用本指南:
Manage Display 管理界面可以更改欄位標籤和內容的顯示方式。以下當本文想展示如何輸出僅管理界面的內容時,我們使用「display」。當本文談到內容的原始值時,使用「value」。
本文已使用 "field_name" 作為欄位的機器名稱。您可以在內容類型的「Manage Fields」中找到機器名稱,並替代"field_name" 。

目錄

  1. Body
  2. Text
  3. List
  4. Link
  5. Image
  6. File
  7. Boolean
  8. Number
  9. Email
  10. Phone
  11. Date
  12. Taxonomy/Entity Reference
  13. Labels for any field type
  14. Multi-value for any field type
  15. Examples
  16. Concepts/Background

1. Body

Content type認為body field是一個基本的Text (formatted, long, with summary)欄位類型。

Content (display): {{ content.body.0 }} 這將顯示在Manage Display中設置的內容。如果body field 設為 Summary 或 Trimmed,則會顯示摘要內容。

Content (value, formatted): {{ node.body.value|raw }} 這將顯示文本自身的內容,不管在Manage Display中設置了什麼。這將解析任何HTML。

Note據了解,Twig過濾器raw可能存在安全問題,但尚未找到另一種方法來執行此操作。

Content (value, unparsed HTML): {{ node.body.0.value }}顯示欄位的原始值,包括未解析的HTML。

Content (value, plain text): {{ node.body.0.value|striptags }} 與上述相同,但將刪除HTML,就像它是純文本一樣。

Summary: {{ node.body.summary }}


2.Text

與body非常相似

Text (plain) and Text (plain, long)

Content (display): {{ content.field_name }}

Content (value): {{content.field_name.0 }}

Text (formatted), Text (formatted, long), Text (formatted, long, with summary)

Content (display): {{ content.field_name.0 }} 再次顯示在Manage Display中設置的內容,可能是 Default Trimmed。

Content (value, formatted): {{ node.field_name.0.value|raw }}

Content (value, unparsed): {{ node.field_name.0.value }}顯示欄位的原始值,包括未解析的HTML。

Content (value, plain text): {{ node.field_name.0.value|striptags }}

Summary: {{ node.field_name.summary }}


3.List

List (float), List (integer), List (text)

這些是具有 key 和 label 的欄位。key 是儲存的值。請記住,這裡的 label 不是指欄位標籤,而是 key 的 label。

Content (display): {{ content.field_name.0 }}如果顯示設置為 Key,則將顯示 key。如果設置為 Label,則將顯示 label。

Content (label):本文尚未找到如何以與提取 key 相同的方式提取 label。因此,您可通過更改Manage Display為Label來執行此操作。

Content (key): {{ node.field_name.0.value }}

這將顯示欄位中的第一個項目。對於多數值欄位,您可以通過將0更改為其他數字來顯示不同的項目。請記住從0開始計數;第二個項目將是1。

要顯示所有項目,請參閱13章。


4.Link

Content (display): {{ content.field_name }}

如果這是個多數值的欄位,上面的示例將輸出所有的值,正如在“Manage Display”中的配置。若要以這種方式提取僅一個值,請添加索引,如下所示: {{ content.field_name.0 }}

Link only: {{ content.field_name.0.url }}

Link text only: {{ content.field_name.0.title }}

這將顯示欄位中的第一個項目。對於多數值欄位,可以通過將0更改為其他數字來顯示不同的項目。請記住從0開始計數,第二個項目將是1。


5.Image

Content (display — the image): {{ content.field_name.0 }}

Content (image path): {{ file_url(content.field_name['#items'].entity.uri.value) }}

Alt text: {{ node.field_name.alt }}

Title text: {{ node.field_name.title }}

Height/Width:

{{ node.field_name.height }} or {{ node.field_name.width }}


6.File

Content (display): {{ content.field_name.0 }}

Content (file path): {{ file_url(content.field_name['#items'].entity.uri.value) }}

Description: {{ node.field_name.description }}


7.Boolean

這個部分和 List 非常相似,但 key 的運作方式略有不同。對於boolean的欄位, key 只有兩個選項,0(未選擇)或1(已選擇)。

Content (display): {{ content.field_name.0 }}

Content (key): {{ node.field_name.0.value }}

若要顯示所有的項目,請參閱14。


8.Number

Number (decimal) and Number (float)

Content (display): {{ content.field_name.0 }}

Content (value): {{ node.field_name.0.value }} This would remove any prefix/suffix.

若要顯示所有的項目,請參閱14。


9.Email

Content (display): {{ content.field_name.0 }}

Content (value): {{ node.field_name.0.value }} This shows the plain text email.

若要顯示所有的項目,請參閱14。


10.Phone

Content (display): {{ content.field_name.0 }}

Content (value): {{ node.field_name.0.value }}

若要顯示所有的項目,請參閱14。


11.Date

Content (display): {{ content.field_name.0 }}

Content (value): {{ node.field_name.0.value }}

這將輸出通常類似於以下的日期信息:週一,2019年03月25日–02:57。

若要顯示所有的項目,請參閱14。


12.Taxonomy/Entity Reference

本文對這個欄位的了解不夠深入,但關於「分類術語參考」(Taxonomy term reference)的部分,本文可以提供一些資訊。

Content (display): {{ content.field_name.0 }}

Content (value): {{ node.field_name.0.target_id }}

這顯示了 tag id,它是一個數字。

Content (term title): {{ content.field_name[0]['#title'] }}

Content (term path): {{ content.field_name[0]['#url'] }}


13.Labels for any field type

{{ node.field_name.fieldDefinition.label }}


14.Multi-value for any field type

當一個欄位是多數值欄位時,以下是如何在 node template 中以多種 HTML 控制輸出。

在這裡,{{ item }} 會按照在 Manage Display 中的配置,顯示完整的欄位內容。以下是如何在 <ul> 中使用它的方式。

{% if content.field_name[0] %}
  <ul>
    {% for key, item in content.field_name if key|first != '#' %}
      <li>{{ item }}</li>
    {% endfor %}
  </ul>
{% endif %}

您可以將這種多數值的技巧與上述的技巧結合使用。


15.Examples

範例1:製作一個 ul,其中包含 List 欄位的 keys。

{% if content.field_name[0] %}
  <ul>
    {% for key, item in node.field_name if key|first != '#' %}
      <li>{{ item.value }}</li>
    {% endfor %}
  </ul>
{% endif %}

  1. 要獲得整個欄位,使用{{ content.field_name }}, 如果想要檢查是否至少有一個值,可以使用 {% if content.field_name[0] %}
  2. 要獲得單個 key,我會使用{{ node.field_name.0.value }}, 並使用 node.field_name 來提取項目。

範例2:在一個 <ul> 中獲取標籤的名稱。

{% if content.field_name[0] %}
  <ul>
    {% for key, item in content.field_name if key|first != '#' %}
      <li>{{ item['#title'] }}</li>
    {% endfor %}
  </ul>
{% endif %}

  1. 要獲得整個欄位,使用{{ content.field_name }}, 如果想要檢查是否存在一個值,可以使用 {% content.field_name[0] %}
  2. 要獲得單一 tag title,使用{{ content.field_name[0]['#title'] }}, 所以在段落可使用欄位的第一部分:content.field_name。
  3. 數字 0 代表 list 中的某個項目,所以我可以用 "item" 代替它以及它之前的所有內容,然後尋找 ['#title']。

16.Concepts/Background

請留意上面提到的 content 和 node 的使用情況。在類似 node--page.html.twig 的 node template中,content variable 會查看在該內容類型的 Manage Display 中所選擇的選項。本文盡可能地在使用者界面中進行操作,但有時我們需要在這些設置之前獲取欄位的部分內容

這就是當我們進入 node 以獲取我們想要的內容時的情況。更多 raw 信息儲存在 Display 之上或之前


以上就是欄位值在Twig模板引擎的應用