首页 > 解决方案 > 是否可以从 amp-list 中的 JSON 呈现 AMP 组件?

问题描述

我正在创建一个 ASP.NET MVC 应用程序来转换来自内容管理系统的内容并将其呈现为符合 AMP 标准的页面。CMS 有一个称为列表的概念,它将特定模板的内容项组合在一起,每个内容项都可以具有不同类型的自定义字段(纯文本、布尔值、HTML 等)。

amp-list 组件允许您使用三重花括号而不是双花括号来呈现原始 HTML,例如{{{htmlProperty}}}. 这通常有效,但我遇到了剥离图像的问题,即使我在服务器端修改它们以使用正确的 amp-img 语法。

我有以下 amp-list 组件:

<amp-list layout="fixed-height" height="500" src="https://jsonblob.com/api/jsonBlob/56eb207e-e2c3-11e8-a8cb-676415a24d50" single-item>
    <template type="amp-mustache">
        {{#contentItems}}
        <section>
            <header>
                {{title}}
            </header>
            <div>
                {{{content}}}
            </div>
        </section>
        {{/contentItems}}
    </template>
</amp-list>

我有以下示例 JSON:

{
  "totalRecords": 2,
  "pageSize": 0,
  "pageNumber": 1,
  "items": {
    "contentItems": [
      {
        "row": 1,
        "title": "What is your favorite sport?",
        "content": "<p><strong>Hockey</strong></p>"
      },
      {
        "row": 2,
        "title": "What is your favorite country?",
        "content": "<p>Canada</p><amp-img src=\"http://www.flagblvd.com/wp-content/uploads/2015/09/canadian-flag-medium.png\" alt=\"\" layout=\"fill\"></amp-img>"
      }
    ]
  }
}

请注意,该title属性是纯文本,content而是 HTML。加载页面时,第<strong>一个内容项中标记的粗体起作用,但如果您在浏览器的开发人员控制台中进行检查,您会注意到输出中省略了 amp-img。请参阅此jsfiddle进行演示。

有没有办法让它正常工作?我在官方文档中没有看到任何关于此的内容。我什至只是在没有相应文档的示例中看到使用三重大括号来呈现原始 HTML 时才发现它。

标签: amp-html

解决方案


"triple-mustache" 的输出被清理为只允许以下标签:a, b, br, caption, colgroup, code, del, div, em, i, ins, li, mark, ol, p, q, s, small, span, , strong, sub, sup, table, tbody, time, td, th, thead, tfoot, tr, .uul

了解更多信息


推荐阅读