当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Google AMP amp-mustache用法及代码示例


我们使用amp-mustache渲染Mustache模板。 amp-mustache不提供由amp-list,amp-access或amp-form组件收集的JSON数据。

使用amp-list与字典一起传递的JSON文件示例。

Javascript

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

必需的脚本:将amp-mustache组件导入标头。



HTML

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

amp-mustache与amp-list或amp-form之类的组件一起使用以传递JSON文件(介绍后给出的示例)。

将amp-list组件导入标头。

HTML

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

将amp-list组件导入标头。

HTML

<script async custom-element="amp-form" src= 
"https://cdn.ampproject.org/v0/amp-form-0.1.js"> 
</script>

验证:amp-mustache必须位于结构良好的DOM片段中。因此,我们不能将amp-mustache用于:

  • 计算标签名称,例如<{{tagName}}>
  • 计算属性名称<div {{attrName}} = attr>

这些是不允许的

例:

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>

输出:

相关用法


注:本文由纯净天空筛选整理自somsagar2019大神的英文原创作品 Google AMP amp-mustache。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。