首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >amp-list中的amp-mustache不解析在mustache部分html文件中使用的mustache变量

amp-list中的amp-mustache不解析在mustache部分html文件中使用的mustache变量
EN

Stack Overflow用户
提问于 2018-12-06 10:27:10
回答 1查看 1.4K关注 0票数 1

我正在渲染我的amp页面与多个胡子部分文件(页眉,页脚,卡片等)。在我的一段代码中,我想循环遍历JSON中给出的一个项目数组,所以我在其中使用了amp-list,我使用的是amp-mustache模板。在输出中,我得到了包含空文本的列表。mustache变量(url和title)没有解析为JSON中给出的值。

我的partial.html

代码语言:javascript
复制
<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

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
<!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>
EN

回答 1

Stack Overflow用户

发布于 2018-12-06 21:02:21

已经在服务器端呈现了mustache变量。您需要对它们进行转义,才能在客户端使用它们。例如,对于Handlebars.js,您可以通过在它们前面加上\来对它们进行转义

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53643717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档