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,压缩后)。 迁移到更现代的 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>
然后,可以像这样在 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>
。
对“三括号”的输出进行清理,仅允许以下标签:
a
、amp-img
、article
、aside
、b
、blockquote
、br
、caption
、code
、col
、colgroup
、dd
、del
、details
、div
、dl
、dt
、em
、figcaption
、figure
、footer
、h1
、h2
、h3
、header
、hr
、i
、ins
、li
、main
、mark
、nav
、ol
、p
、pre
、q
、s
、section
、small
、span
、strong
、sub
、summary
、sup
、table
、tbody
、td
、tfoot
、th
、thead
、time
、tr
、u
、ul
。
清理
出于安全原因和维护 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>
表格
由于 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>
在 {{foo}}
或 {{bar}}
变量中使用 HTML 字符代码不起作用,因为 Mustache 会对 &
字符进行 HTML 转义(例如,"
-> &quot;
)。一种解决方法是使用传真字符,例如 ′ (′
) 和 ″ (″
)。
HTML 实体
HTML 实体在 <template>
元素中不会保留。
如果你想在服务器端渲染包含用户生成文本的 <template>
,这可能会成为一个问题,因为包含 {{
、}}
、{{{
、}}}
的用户生成文本将被视为 Mustache 节。例如,将 {{
替换为 HTML 实体 {{
将不起作用,因为当浏览器解析 <template>
时,它们不会被保留。
解决方法包括将 {{
之类的字符串替换为不同的字符,或者直接从用户生成的内容中删除它们。
验证
请参阅 AMP 验证器规范中的 amp-mustache 规则。
你已经阅读本文档十几次了,但它并没有真正涵盖你所有的问题?也许其他人也有同感:在 Stack Overflow 上联系他们。
转到 Stack Overflow 发现错误或缺少功能?AMP 项目强烈鼓励你的参与和贡献!我们希望你成为我们开源社区的长期参与者,但也欢迎你对你特别热衷的问题做出一次性贡献。
转到 GitHub