简体   繁体   English

amp-list中的amp-mustache在小胡子部分html文件中使用时无法解析小胡子变量

[英]amp-mustache within amp-list not resolving the mustache variables when used in a mustache partial html file

I am rendering my amp page with multiple mustache partial files(header, footer, cards etc). 我正在用多个小胡子局部文件(页眉,页脚,卡片等)渲染我的amp页面。 In one of my partial, I want to loop through an array of items given in a JSON, so I am using amp-list within that I am using amp-mustache template. 在我的部分内容中,我想遍历JSON中给出的一系列项目,因此我使用的是amp-mustache模板中的amp-list。 In the output, I am getting the list with empty text. 在输出中,我得到的列表中包含空文本。 The mustache variables (url and title) are not resolved to the values given in the JSON. 小胡子变量(URL和标题)未解析为JSON中给定的值。

My partial.html 我的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>

and my examples.json 和我的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/"
    }
  ]
 }

and here is my index.html 这是我的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>

The mustache variables are already rendered server-side. 小胡子变量已在服务器端呈现。 You need to escape them for them to be available client-side. 您需要对其进行转义,以使其在客户端可用。 For Handlebars.js you can, for example, escape mustache variable by prefixing them with \\ : 例如,对于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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM