1
votes

I am rendering my amp page with multiple mustache partial files(header, footer, cards etc). 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. 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.

My 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

{
  "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

<!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>
1
I think it is issue with amp : github.com/ampproject/amphtml/issues/5206Bachcha Singh
I have the same problem, The html is being displayed with no values, how did you manage to solve this?Arpan Jain

1 Answers

0
votes

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 \:

<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>