
We use amp-mustache to render Mustache templates. The amp-mustache does not provide the JSON data they are collected by amp-list, amp-access or amp-form component.
Example of a JSON file passed with a dictionary using amp-list.
Javascript
{ "geeksforgeeks": [ { "fullname": "geeksforgeeks", "phonenumber": "9999999999", } ]} |
Required Script: Importing the amp-mustache component into the header.
HTML
<script async custom-template="amp-mustache" src=</script> |
The amp-mustache is used with components like amp-list or amp-form to pass a JSON file (example given after introduction).
Importing amp-list component into the header.
HTML
<script async custom-element="amp-list" src=</script> |
Importing amp-list component into the header.
HTML
<script async custom-element="amp-form" src=</script> |
Validation: The amp-mustache is needed to be in well framed DOM fragments. So we can’t use amp-mustache for:
- Calculate tag name eg <{{tagName}}>
- Calculate attribute name <div {{attrName}}=attr>
These are not allowed
Example:
HTML
<!doctype html><html ⚡> <head> <meta charset="utf-8"> <title>Google AMP amp-mustache</title> <link rel="canonical" href= <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"> <script async src= </script> <!--Import the `amp-mustache` tag.--> <script async custom-template="amp-mustache" </script> <script async custom-element="amp-list" </script> <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> <style amp-custom> </style></head> <body> <amp-list src="geeks.json" layout="fixed-height" height="50" binding="no"> <template type="amp-mustache"> {{fullname}}! {{#phonenumber}} your phone number is {{phonenumber}} {{/phonenumber}} </template> </amp-list></body> </html> |
Output:


