Open In App

Google AMP amp-mustache

Last Updated : 25 Oct, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

 

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=
"https://cdn.ampproject.org/v0/amp-mustache-0.2.js">
</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=
"https://cdn.ampproject.org/v0/amp-list-0.1.js">
</script>

 

Importing amp-list component into the header.

HTML
<script async custom-element="amp-form" src=
"https://cdn.ampproject.org/v0/amp-form-0.1.js">
</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=
"https://amp.dev/documentation/examples/components/amp-mustache/index.html">

    <meta name="viewport" content=
"width=device-width,minimum-scale=1,initial-scale=1">

    <script async src=
        "https://cdn.ampproject.org/v0.js">
    </script>
    
    <!--Import the `amp-mustache` tag.-->
    <script async custom-template="amp-mustache" 
src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js">
    </script>

    <script async custom-element="amp-list" 
src="https://cdn.ampproject.org/v0/amp-list-0.1.js">
    </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:


Similar Reads