利用運算式片段 use-expression-fragments

在此頁面上:​瞭解如何在個人化編輯器中插入及重複使用運算式片段、使用隱含變數、在回圈中使用片段、自訂可編輯欄位,以及中斷Adobe Journey Optimizer中的繼承。

使用​ 個人化編輯器 ​時,您可以利用所有已建立或已儲存至目前沙箱的運算式片段。

片段是可重複使用的元件,可跨Journey Optimizer個行銷活動和歷程參照。 此功能允許預先建置多個自訂內容區塊,可供行銷使用者在改良的設計流程中快速組合內容。 進一步瞭解片段

➡️ 在此影片中瞭解如何管理、編寫和使用片段

使用運算式片段 use-expression-fragment

若要將運算式片段新增至您的內容,請遵循下列步驟。

NOTE
您最多可以在給定傳送中新增30個片段。 片段最多只能巢狀1個層級。
  1. 開啟個人化編輯器並在左窗格上選取​ 片段 ​按鈕。

    清單會顯示目前沙箱上已建立或儲存為片段的所有運算式片段。 瞭解如何建立片段
    它們會依建立日期排序:最近新增的運算式片段會先顯示在清單中。

    您也可以重新整理此清單。

    note
    NOTE
    如果您在編輯內容時修改或新增了某些片段,清單會以最新變更更新。
  2. 將游標置於內容中所需的位置,然後按一下運算式片段旁的+圖示,將對應的片段ID插入編輯器中。

    note
    NOTE
    您也可以新增運算式片段至電子郵件或內容範本的<head>區段。

    note caution
    CAUTION
    您可以將任何​ 草稿 ​或​ 即時 ​片段新增至您的內容。 但是,如果歷程或行銷活動中使用了具有​ 草稿 ​狀態的片段,則您將無法啟用該歷程或行銷活動。 在歷程或行銷活動發佈中,草稿片段將顯示錯誤,您需要核准它們才能發佈。
  3. 在新增片段ID後,如果您開啟對應的運算式片段並從介面編輯它,變更便會同步。 它們會自動傳播到包含該片段ID的所有草稿或即時歷程/行銷活動。

  4. 按一下片段旁的​ 更多動作 ​按鈕。 從開啟的關聯功能表中,選取​ 檢視片段 ​以取得有關該片段的詳細資訊。 也會顯示​片段ID,可從此處複製。

  5. 您可以在另一個視窗中開啟運算式片段,以編輯其內容和屬性 — 使用內容功能表中的​ 開啟片段 ​選項或從​ 片段資訊 ​窗格。 瞭解如何編輯片段

  6. 然後您就可以照常使用個人化編輯器的所有個人化和編寫功能,自訂及驗證您的內容。

  7. 某些情況下,您只需要計算變數,因此您可能想要隱藏運算式片段的內容。 若要這麼做,請使用render屬性並將其設定為false。 例如:

    code language-none
    Hi {{profile.person.name.firstName|fragment id='ajo:fragmentId/variantId' mode ='inline' render=false}}
    
NOTE
如果您建立包含多個分行符號的運算式片段,並將其用於簡訊推播內容,則會保留分行符號。 因此,在傳送您的簡訊推播訊息之前,請務必先測試該訊息。

使用隱含變數 implicit-variables

隱含變數可增強現有片段功能,以提升內容重複使用性及指令碼使用案例的效率。 片段可以使用輸入變數並建立可用於行銷活動和歷程內容的輸出變數。

例如,此功能可用於根據目前的行銷活動或歷程,初始化電子郵件的追蹤引數,並將這些引數用於新增至電子郵件內容的個人化連結。

可以使用的使用案例如下:

  1. 在片段中使用輸入變數。

    當片段用於行銷活動/歷程動作內容時,它有能力運用在片段外部宣告的變數。 範例如下:

    我們可以看見以上在行銷活動內容中宣告utm_content變數。 使用片段​ Hero區塊 ​時,將顯示要附加utm_content引數值的連結。 最終結果為: https://luma.enablementadobe.com?utm_campaign= Product_launch&utm_content= start_shopping

  2. 使用來自片段的輸出變數。

    在片段中計算或定義的變數可用於您的內容。 在以下範例中,片段​ F1 ​宣告了一組變數:

    在電子郵件內容中,您可以進行下列個人化:

    片段F1初始化下列變數: utm_campaignutm_content。 然後,訊息內容中的連結會附加這些引數。 最終結果為: https://luma.enablementadobe.com?utm_campaign= Product_launch&utm_content= start_shopping

NOTE
在執行階段,系統會展開片段內的內容,然後從上到下解譯個人化程式碼。 請記住,完成更複雜的使用案例。 例如,您可以有片段F1將變數傳遞至下方的另一個片段F2。 您也可以讓視覺化片段F1將變數傳遞至巢狀運算式片段F2。

在回圈中使用運算式片段 fragments-in-loops

{{#each}}回圈中使用運算式片段時,請務必瞭解變數範圍的運作方式。 運算式片段可以存取訊息內容中定義的全域變數,但是這些片段無法接收回圈特定的變數做為引數。

支援的模式:使用全域變數 global-variables-in-loops

運算式片段可參考在片段外部定義的全域變數,即使從回圈內呼叫片段亦然。 當您需要使用反複內容中的片段時,這是建議的方法。

範例:在回圈內使用具有全域變數的片段

在您的訊息內容中,定義全域變數並使用參考它的片段:

{% let globalDiscount = 15 %}

{{#each context.journey.actions.GetProducts.items as |product|}}
  <div class="product">
    <h3>{{product.name}}</h3>
    <p>Price: ${{product.price}}</p>
    {{fragment id='ajo:fragment123/variant456' mode='inline'}}
  </div>
{{/each}}

在運算式片段(fragment123)中,您可以參考globalDiscount變數:

<p class="discount-info">Save {{globalDiscount}}% on all items!</p>

此模式之所以能運作,是因為無論回圈內容為何,整個訊息(包括片段內)皆可存取全域變數。

不支援:傳遞回圈變數做為片段引數 loop-variables-limitations

您無法將目前反複專案(例如上述範例中的product)以引數形式傳遞至運算式片段。 片段無法從周圍的{{#each}}區塊直接存取回圈範圍的變數。

範例:什麼不運作

{{#each context.journey.actions.GetProducts.items as |product|}}
  <!-- This will NOT work as expected -->
  {{fragment id='ajo:fragment123/variant456' mode='inline' currentProduct=product}}
{{/each}}

片段無法接收product做為引數,並在內部使用,因為目前的實作不支援為回圈特定變數傳遞引數。

建議的因應措施 fragments-in-loops-workarounds

當您需要將運算式片段用於回圈中的資料時,請考慮以下方法:

  1. 直接在訊息中包含邏輯:不要將片段用於回圈特定的邏輯,而是直接在您的{{#each}}區塊中新增個人化程式碼。

    code language-handlebars
    {{#each context.journey.actions.GetProducts.items as |product|}}
      <div class="product">
        <h3>{{product.name}}</h3>
        <p>Price: ${{product.price}}</p>
        {{#if product.price > 100}}
          <span class="premium-badge">Premium Product</span>
        {{/if}}
      </div>
    {{/each}}
    
  2. 使用回圈以外的片段:如果片段內容不是回圈相依的,請在反複專案區塊之前或之後呼叫片段。

    code language-handlebars
    {{fragment id='ajo:fragment123/variant456' mode='inline'}}
    
    {{#each context.journey.actions.GetProducts.items as |product|}}
      <div class="product">
        <h3>{{product.name}}</h3>
        <p>Price: ${{product.price}}</p>
      </div>
    {{/each}}
    
  3. 設定多個全域變數:如果您需要跨反複專案傳遞不同的值給片段,請在每個片段呼叫之前設定全域變數(不過這會限制彈性)。

NOTE
若要反複運算關聯式資料和使用回圈,請參閱反複運算關聯式資料的完整指南,其中包括最佳實務、疑難排解提示和進階模式。

自訂可編輯欄位 customize-fields

如果運算式片段的某些部分已使用變數設為可編輯,您可以使用特定語法覆寫其預設值。 瞭解如何使您的片段可自訂

若要自訂欄位,請執行下列步驟:

  1. 從​ 片段 ​功能表將片段插入您的程式碼中。

  2. 在語法結尾使用<fieldId>="<value>"程式碼以覆寫變數的預設值。

    在以下範例中,我們以「yoga」值覆寫ID為「sports」之變數的值。 只要引用「sport」變數,片段內容中就會顯示「瑜伽」。

本節提供範例,說明如何在建立電子郵件時,將可編輯欄位新增至運算式片段並覆寫其值。

使用動態片段解析 dynamic-resolution

您可以在執行階段為每個收件者動態解析片段ID,而不是在設計時靜態嵌入片段ID。 這可讓不同的設定檔根據設定檔屬性、資料集查詢或內容資料,在相同行銷活動或歷程中接收完全不同的內容區塊。

瞭解如何使用動態片段

中斷繼承 break-inheritance

將片段ID新增至個人化編輯器時,會同步對原始運算式片段所做的變更。

不過,您也可以將運算式片段的內容貼到編輯器中。 從內容功能表中,選取​ 貼上片段 ​以插入該內容。

在這種情況下,來自原始片段的繼承會中斷。 片段內容會複製到編輯器中,且變更不再同步。

它會變成不再連結至原始片段的獨立元素;您可以像在程式碼中的任何其他元素一樣加以編輯。

AI Knowledge Reference

This section contains structured knowledge intended to support interpretation, retrieval, and question answering related to this topic.

For complete understanding, this information should be combined with the documentation on this page. Neither source is intended to stand alone; the page describes the feature, while this section provides additional context that helps disambiguate terminology, intent, applicability, and constraints.

  • TL;DR: This page explains how to insert, customize, and manage expression fragments in the personalization editor — including implicit variables, using fragments inside loops, editable fields, dynamic resolution, and breaking inheritance.

Intents:

  • Insert an expression fragment from the Fragments menu and understand automatic change propagation
  • Use implicit variables: input variables (declared outside the fragment, used inside) and output variables (declared inside the fragment, used in surrounding message content)
  • Use expression fragments inside loops — leverage global variables for fragment access; understand the limitation on passing loop-scoped variables as parameters
  • Override editable fields in a customizable fragment using <fieldId>="<value>" syntax
  • Resolve fragment IDs dynamically at runtime based on profile attributes, dataset lookups, or context data
  • Break inheritance by pasting fragment content directly into the editor

Glossary:

  • Expression fragment: A reusable personalization expression component referenced by ID across campaigns and journeys; changes to the fragment propagate automatically to all content referencing it. (product-specific)
  • Implicit variables: Variables that extend the fragment functionality — input variables (declared in campaign/journey content, consumed inside the fragment) and output variables (declared inside the fragment, available in the surrounding message content). (product-specific)
  • Input variable: A variable declared outside the fragment (in campaign or journey content) that the fragment can reference and use internally.
  • Output variable: A variable declared or computed inside a fragment that becomes available for use in the surrounding message content after the fragment is called.
  • Editable fields: Fragment variables exposed to allow the inserting user to override default values using <fieldId>="<value>" syntax, without editing the fragment source. (product-specific)
  • Dynamic fragment resolution: The ability to resolve a fragment ID at runtime (based on profile attributes, dataset lookups, or context data) rather than embedding a static fragment ID at design time. (product-specific)
  • Break inheritance: Using “Paste fragment” from the contextual menu copies the fragment’s content into the editor as a standalone element that no longer synchronizes with the original fragment. (product-specific)

Guardrails:

  • Maximum 30 fragments can be added in a given delivery.
  • Fragments can only be nested up to 1 level.
  • A journey or campaign cannot be activated or published if it contains a fragment with Draft status; draft fragments must be approved before publication.
  • Expression fragments cannot receive loop-scoped variables (the current {{#each}} iteration item) as parameters — this is a known limitation. Use global variables or inline logic as a workaround.
  • If a fragment containing multiple line breaks is used in SMS or push content, line breaks are preserved; test the content before sending.

Terminology:

  • Canonical name: expression fragment — variants: fragment, expression fragment
  • Synonyms: “fragment ID” = the identifier used to reference the fragment in expressions
  • Do not confuse: inserting a fragment by ID (referenced; changes propagate automatically to all content) ≠ breaking inheritance / paste fragment (content copied into editor; standalone element, no longer linked to original)
  • Do not confuse: input variables (declared outside the fragment, consumed inside) ≠ output variables (declared inside the fragment, consumed outside in surrounding message content)
  • Do not confuse: Draft fragment (can be added to content but blocks journey/campaign publication until approved) ≠ Live fragment (fully published; safe for active journeys and campaigns)

FAQ:

  • Q: How many fragments can be added in a single delivery? — Up to 30 fragments.
  • Q: Can fragments be nested inside other fragments? — Yes, but only up to 1 level of nesting.
  • Q: What happens if I use a Draft fragment in a journey or campaign? — You can add a Draft fragment to content, but you cannot activate or publish the journey or campaign until the fragment is approved and its status changes to Live.
  • Q: Can an expression fragment receive the current loop item (e.g., product in {{#each}}) as a parameter? — No. Expression fragments cannot receive loop-scoped variables as parameters. Use global variables declared outside the loop (which the fragment can access), or include the personalization logic directly within the loop instead of using a fragment.
  • Q: What is breaking inheritance and when should I use it? — Breaking inheritance means using “Paste fragment” from the contextual menu to copy the fragment’s content directly into the editor. The pasted content becomes a standalone element that no longer synchronizes with the original fragment — use this when you need to customize the content beyond what editable fields allow, knowing future changes to the original fragment will not propagate to this copy.
recommendation-more-help
journey-optimizer-help