【问题标题】:AMP for emails: How to handle complex conditional logic in AMP for emails?用于电子邮件的 AMP:如何处理用于电子邮件的 AMP 中的复杂条件逻辑?
【发布时间】:2021-07-25 16:57:17
【问题描述】:

我开发了一个代码,它使用 amp-list 从后端服务器检索 json 数据。基于来自服务器的变量之一,我想要一个 if 条件。下面提到了我希望完成的示例:

<html ⚡4email data-css-strict>
<head>
  <meta charset="utf-8">
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
  <script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
  <style amp4email-boilerplate>body{visibility:hidden}</style>
  <style amp-custom>
    h1 {
      margin: 1rem;
    }
  </style>
</head>
<body>
  <amp-list template="list-template" src="https://random_server_link/get_x_value_and_y_value" layout="responsive">
    <template id="list-template" type="amp-mustache">
    {% if {{x}} == {{y}} %}
      <p>
          x == y
      </p>  
    {% elif {{x}} != {{y}} %}
      <p>
        x not equal to y
      </p>
    {% else %}
      <p>
        else condition
      </p>
{% endif %}
  </template>
  </amp-list>
  <h1>Hello, I am an AMP EMAIL!</h1>
</body>
</html>

上面的代码不起作用,我需要一个替代 if-else 条件。

谢谢

【问题讨论】:

    标签: html amp-email


    【解决方案1】:

    对于简单的 if/else 测试 Mustache 变量的真实性,您可以使用部分标记和倒置部分标记,如 AMP documentationMustache documentation 中所述。有关示例,请参阅How do I accomplish an if/else in mustache.js?

    对于比简单的真实性测试更复杂的事情(例如,两个变量之间的相等性检查),您必须将该逻辑推送到服务器端。 Mustache 不具备比较值或表达两个变量之间的任何其他二元关系的能力。它被设计成一个无逻辑的模板。在服务器端进行计算,将其转换为简单的位,您可以在客户端使用 Mustache 中的节标记和倒置节标记进行检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      相关资源
      最近更新 更多