AMP

amp-mustache

描述

允许渲染 Mustache 模板。

 

必需脚本

<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.1.js"></script>

版本说明

版本 描述
0.2 支持 SVG 元素并减小捆绑包大小(12.2KB 对比 20.5KB,gzip 压缩)。

迁移到更现代化的 HTML 清理库 (Caja 到 DOMPurify)。由于标记和属性允许列表中的差异,这可能会导致轻微的破坏性更改。我们建议您在推送到生产环境之前先测试您的页面,以确保生成的标记中的更改不会影响功能。
0.1 初始实现。

语法

Mustache 是一种无逻辑的模板语法。有关更多详细信息,请参阅Mustache 规范。一些核心 Mustache 标签是

  • {{variable}}:变量标签。它输出变量的 HTML 转义值。
  • {{#section}} {{/section}}:节标签。它可以测试变量的存在,如果变量是数组,则可以对其进行迭代。
  • {{^section}} {{/section}}:反向标签。它可以测试变量的不存在。
  • {{{unescaped}}}:未转义的 HTML。它可能输出的标记受到限制(请参阅下面的“限制”)。

用法

amp-mustache 模板必须按照AMP 模板规范进行定义和使用。

首先,必须像这样声明/加载 amp-mustache

<script
  async
  custom-template="amp-mustache"
  src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"
></script>

然后,可以在 scripttemplate 标签中定义 Mustache 模板,如下所示

<!-- Using template tag. -->
<template type="amp-mustache">
  Hello {{world}}!
</template>

<script type="text/plain" template="amp-mustache">
  Hello {{world}}!
</script>

尽可能使用 template 标签,因为 AMP 验证提供了有用的开发提示。对于表格上下文中的边缘情况和模板问题,请使用 script 模板。请参阅下面的“表格”部分。

如何发现模板、何时渲染模板、如何提供数据,所有这些都由目标 AMP 元素决定,该元素使用此模板来渲染其内容(例如,在amp-listamp-form 等中)。

限制

验证

与所有 AMP 模板一样,amp-mustache 模板必须是格式良好的 DOM 片段。这意味着,除其他事项外,您不能使用 amp-mustache

  • 计算标签名称。例如,不允许使用 <{{tagName}}>
  • 计算属性名称。例如,不允许使用 <div {{attrName}}=something>

“三重大括号”的输出经过清理,只允许以下标签

aamp-imgarticleasidebblockquotebrcaptioncodecolcolgroupdddeldetailsdivdldtemfigcaptionfigurefooterh1h2h3headerhriinslimainmarknavolppreqssectionsmallspanstrongsubsummarysuptabletbodytdtfootththeadtimetruul

清理

为了安全原因和保持 AMP 有效性,Mustache 输出已清理。这可能会导致某些元素和属性被静默删除。

陷阱

嵌套模板

根据 AMP 验证,<template> 元素不能是其他 <template> 元素的子元素。当嵌套两个使用模板的组件时,可能会发生这种情况,例如 amp-listamp-form

要解决此问题,还可以通过组件上的 template 属性通过 id 引用 <template> 元素。例如

<amp-list id="myList" src="https://foo.com/list.json">
  <template type="amp-mustache">
    <div>{{title}}</div>
  </template>
</amp-list>

也可以表示为

<!-- Externalize templates to avoid nesting. -->
<template type="amp-mustache" id="myTemplate">
  <div>{{title}}</div>
</template>

<amp-list id="myList" src="https://foo.com/list.json" template="myTemplate">
</amp-list>

表格

由于 AMP 模板字符串必须在 <template> 元素中指定,这可能会因浏览器解析而导致意外行为。例如,<table> 元素可能会导致文本的寄养。在以下示例中

<template type="amp-mustache">
  <table>
    <tr>
{{#foo}}      <td></td>
{{/foo}}    </tr>
  </table>
</template>

浏览器将寄养文本节点 {{#foo}}{{/foo}}

{{#foo}} {{/foo}}<table>
  <tr>
    <td></td>
  </tr>
</table>

解决方法包括将 Mustache 节包装在 HTML 注释中(例如 <!-- {{#bar}} -->),使用非表格元素(如 <div>)或使用 <script type="text/plain"> 标签定义您的模板。

<script type="text/plain" template="amp-mustache">
  <table>
    <tr>
{{#foo}}<td></td>{{/foo}}
    </tr>
  </table>
</script>

引号转义

当使用 amp-mustache 计算属性值时,引号转义可能会成为问题。例如

<template type="amp-mustache">
  <!-- A double-quote (") in foo will cause malformed HTML. -->
  <amp-img alt="{{foo}}" src="example.jpg" width="100" height="100"></amp-img>

  <!-- A single-quote (') or double-quote (") in bar will cause an AMP runtime parse error. -->
  <button on="tap:AMP.setState({foo: '{{bar}}'})">Click me</button>
</template>

由于 Mustache 将 HTML 转义 & 字符(例如,&quot; -> &amp;quot;),因此在 {{foo}}{{bar}} 变量中使用 HTML 字符代码不起作用。一种解决方法是使用传真字符,例如 ′ (&prime;) 和 ″ (&Prime;)。

HTML 实体

HTML 实体不会保留在 <template> 元素中。

如果您想在服务器端渲染包含用户生成文本的 <template> 时,这可能会成为问题,因为包含 {{}}{{{}}} 的用户生成文本将被视为 Mustache 节。例如,将 {{ 替换为 HTML 实体 &lcub;&lcub; 将不起作用,因为当浏览器解析 <template> 时,它们不会被保留。

解决方法包括将 {{ 等字符串替换为不同的字符或直接从用户生成的内容中删除它们。

验证

请参阅 AMP 验证器规范中的amp-mustache 规则

需要更多帮助?

您已经阅读此文档十几次了,但它并没有真正涵盖您的所有问题?也许其他人也有同感:在 Stack Overflow 上联系他们。

转到 Stack Overflow
发现错误或缺少功能?

AMP 项目强烈鼓励您的参与和贡献!我们希望您能成为我们开源社区的长期参与者,但我们也欢迎您对您特别感兴趣的问题做出一次性贡献。

转到 GitHub