辅助程序 gs-helpers

默认回退值 default-value

如果属性为空或null,则使用Default Fallback Value帮助程序返回默认回退值。 此机制适用于配置文件属性和历程事件。

语法

Hello {%=profile.personalEmail.name.firstName ?: "there" %}!

在此示例中,如果此配置文件的firstName属性为空或null,则显示值there

条件 if-function

if帮助程序用于定义条件块。
如果表达式求值返回true,则会呈现块,否则将跳过该块。

语法

{%#if contains(profile.personalEmail.address, ".edu")%}
<a href="https://www.adobe.com/academia">Check out this link</a>

if帮助程序之后,您可以输入else语句以指定要执行的代码块(如果相同条件为false)。
elseif语句将指定一个新条件来测试第一个语句是否返回false。

格式

{
    {
        {%#if condition1%} element_1
        {%else if condition2%} element_2
        {%else%} default_element
        {%/if%}
    }
}

示例

  1. 根据条件表达式呈现不同的存储链接

    code language-sql
    {%#if profile.homeAddress.countryCode = "FR"%}
    <a href="https://www.somedomain.com/fr">Consultez notre catalogue</a>
    {%else%}
    <a href="https://www.somedomain.com/en">Checkout our catalogue</a>
    {%/if%}
    
  2. 确定电子邮件地址扩展名

    code language-sql
    {%#if contains(profile.personalEmail.address, ".edu")%}
    <a href="https://www.adobe.com/academia">Checkout our page for Academia personals</a>
    {%else if contains(profile.personalEmail.address, ".org")%}
    <a href="https://www.adobe.com/orgs">Checkout our page for Non Profits</a>
    {%else%}
    <a href="https://www.adobe.com/users">Checkout our page</a>
    {%/if%}
    
  3. 添加条件链接

    以下操作将添加一个链接,指向仅包含“.edu”电子邮件地址的用户档案的“www.adobe.com/academia'网站”、包含“.org”电子邮件地址的用户档案的“www.adobe.com/org'网站”,以及所有其他用户档案的默认URL“www.adobe.com/users'”:

    code language-sql
    {%#if contains(profile.personalEmail.address, ".edu")%}
    <a href="https://www.adobe.com/academia">Checkout our page for Academia personals</a>
    {%else if contains(profile.personalEmail.address, ".org")%}
    <a href="https://www.adobe.com/orgs">Checkout our page for Non Profits</a>
    {%else%}
    <a href="https://www.adobe.com/users">Checkout our page</a>
    {%/if%}
    
  4. 基于受众成员资格的条件内容

    code language-sql
    {%#if profile.segmentMembership.get("ups").get("5fd513d7-d6cf-4ea2-856a-585150041a8b").status = "existing"%}
    Hi! Esteemed gold member. <a href="https://www.somedomain.com/gold">Checkout your exclusive perks </a>
    {%else%} if 'profile.segmentMembership.get("ups").get("5fd513d7-d6cf-4ea2-856a-585150041a8c").status = "existing"'%}
    Hi! Esteemed silver member. <a href="https://www.somedomain.com/silver">Checkout your exclusive perks </a>
    {%/if%}
    
NOTE
要了解有关受众和分段服务的更多信息,请参阅此部分

Unless unless

unless帮助程序用于定义条件块。 通过与if帮助程序相对,如果表达式求值返回false,则呈现块。

语法

{%#unless unlessCondition%} element_1 {%else%} default_element {%/unless%}

示例

根据电子邮件地址扩展呈现某些内容:

{%#unless endsWith(profile.personalEmail.address, ".edu")%}
Some Normal Content
{%else%}
Some edu specific content Content
{%/unless%}

Each each

each辅助函数用于遍历数组。
辅助函数的语法为{{#each ArrayName}} YourContent {{/each}}
我们可以在块中使用关键字​ this ​引用单个数组项。 可以使用{{@index}}呈现数组元素的索引。

语法

{{#each profile.productsInCart}}
    <li>{{this.name}}</li>
    </br>
{{/each}}

示例

{{#each profile.homeAddress.city}}
  {{@index}} : {{this}}<br>
{{/each}}

示例

呈现此用户在其购物车中拥有的产品列表:

{{#each profile.products as |product|}}
    <li>{{product.productName}} {{product.productRating}}</li>
   </br>
{{/each}}

With with

with帮助程序用于更改模板部分的评估令牌。

语法

{{#with profile.person.name}}
{{this.firstName}} {{this.lastName}}
{{/with}}

with辅助函数也可用于定义快捷键变量。

示例

与一起使用,将长变量名称别名更改为短变量名称:

{{#with profile.person.name as |name|}}
 Hi {{name.firstName}} {{name.lastName}}!
 Checkout our trending products for today!
{{/with}}

Let let

let函数允许将表达式存储为变量,以便稍后在查询中使用。

语法

{% let variable = expression %} {{variable}}

示例

以下示例允许您计算购物车中价格在100和1000之间的产品的总价。

{% let sum = 0%}
    {{#each profile.productsInCart as |p|}}
        {%#if p.price>100 and p.price<1000%}
            {%let sum = sum + p.price %}
        {%/if%}
    {{/each}}
{{sum}}
recommendation-more-help
b22c9c5d-9208-48f4-b874-1cefb8df4d76