重要提示:此文档不适用于您当前选择的格式 故事!
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 模板规范 定义和使用 amp-mustache
模板。
首先,必须像这样声明/加载 amp-mustache
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js" ></script>
然后,可以像这样在 script
或 template
标签中定义 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-list、amp-form 等中)。
限制
验证
像所有 AMP 模板一样,amp-mustache
模板必须是格式良好的 DOM 片段。这意味着,除此之外,您不能使用 amp-mustache
来
- 计算标签名称。例如,不允许使用
<{{tagName}}>
。 - 计算属性名称。例如,不允许使用
<div {{attrName}}=something>
。
对“三层 mustache”的输出进行清理,仅允许以下标签
清理
出于安全原因和维护 AMP 有效性,将清理 Mustache 输出。这可能会导致某些元素和属性被静默删除。
陷阱
嵌套模板
根据 AMP 验证,<template>
元素不能是其他 <template>
元素的子元素。当嵌套两个使用模板的组件(例如 amp-list
和 amp-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>
表格
由于必须在 <template>
元素中指定 AMP 模板字符串,这可能会由于浏览器解析而导致意外行为。例如,<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>
在 {{foo}}
或 {{bar}}
变量中使用 HTML 字符代码不起作用,因为 Mustache 将 HTML 转义 &
字符(例如 "
-> &quot;
)。一种解决方法是使用传真字符,例如 ′ (′
) 和 ″ (″
)。
HTML 实体
HTML 实体不会在 <template>
元素中保留。
如果您想服务端渲染包含用户生成文本的 <template>
,这可能会成为问题,因为包含 {{
、}}
、{{{
、}}}
的用户生成文本将被视为 Mustache 部分。例如,将 {{
替换为 HTML 实体 {{
不起作用,因为当浏览器解析 <template>
时它们不会被保留。
解决方法包括将 {{
之类的字符串替换为不同的字符,或者直接从用户生成的内容中删除它们。
验证
请参阅 AMP 验证器规范中的 amp-mustache 规则。
您已经阅读了本文档十几次,但它并没有真正涵盖您的所有问题?也许其他人也有同样的感受:在 Stack Overflow 上联系他们。
转到 Stack Overflow 发现错误或缺少功能?AMP 项目强烈鼓励您的参与和贡献!我们希望您能成为我们开源社区的持续参与者,但我们也欢迎您为自己特别关注的问题做出一次性贡献。
转到 GitHub