amp-mustache
描述
允许渲染 Mustache 模板。
必需脚本
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
版本说明
版本 | 描述 |
---|---|
0.2 | 支持 SVG 元素并减小了捆绑包大小(12.2KB vs. 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>
。
对“三重大括号”的输出进行清理,只允许以下标签
a
、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