Skip to content
Related Articles

Related Articles

Google AMP amp-mustache
  • Last Updated : 25 Oct, 2020

 

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

filter_none

edit
close

play_arrow

link
brightness_4
code

{
  "geeksforgeeks": [
    {
      "fullname": "geeksforgeeks",
      "phonenumber": "9999999999",
    }
  ]
}

chevron_right


 

Required Script: Importing the amp-mustache component into the header.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<script async custom-template="amp-mustache" src=
</script>

chevron_right


 

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<script async custom-element="amp-list" src=
</script>

chevron_right


 

Importing amp-list component into the header.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<script async custom-element="amp-form" src=
</script>

chevron_right


 

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :