我正在用多个小胡子局部文件(页眉,页脚,卡片等)渲染我的amp页面。在我的部分内容中,我想遍历JSON中给出的一系列项目,因此我使用的是amp-mustache模板中的amp-list。在输出中,我得到的列表中包含空文本。小胡子变量(URL和标题)未解析为JSON中给出的值。
我的partial.html
<ul>
<amp-list width="auto" height="100" layout="fixed-height" src="https://ampbyexample.com/json/examples.json" class="m1">
<template type="amp-mustache" id="amp-template-id">
<li><a href="{{url}}">Link - {{title}}</a></li>
</template>
</amp-list>
</ul>
和我的examples.json
{
"items": [
{
"title": "amp-carousel",
"url": "/components/amp-carousel/"
},
{
"title": "amp-img",
"url": "/components/amp-img/"
},
{
"title": "amp-ad",
"url": "/components/amp-ad/"
},
{
"title": "amp-accordion",
"url": "/components/amp-accordion/"
}
]
}
这是我的index.html
<!doctype html>
<html ⚡ lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<link rel="author" content="Uxmint" href=""/>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Example</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link rel="canonical" href="{{{projectUrl}}}" />
<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
</head>
<body>
{{> partial}}
</body>
</html>
答案 0 :(得分:0)
髭变量已在服务器端呈现。您需要对其进行转义,以使其在客户端可用。例如,对于Handlebars.js,您可以使用\
作为前缀来转义小胡子变量:
<amp-list width="auto" height="100" layout="fixed-height" src="https://ampbyexample.com/json/examples.json" class="m1">
<template type="amp-mustache" id="amp-template-id">
<li><a href="\{{url}}">Link - \{{title}}</a></li>
</template>
</amp-list>