我們使用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>
輸出:
相關用法
- Google AMP amp-ad用法及代碼示例
- Google AMP amp-accordion用法及代碼示例
- Google AMP amp-facebook-like用法及代碼示例
- Google AMP amp-carousel用法及代碼示例
- Google AMP amp-date-countdown用法及代碼示例
- Google AMP amp-brightcove用法及代碼示例
- Google AMP amp-bind-recaptcha用法及代碼示例
- Google AMP amp-autocomplete用法及代碼示例
- Google AMP amp-img用法及代碼示例
- Google AMP amp-image-lightbox用法及代碼示例
- Google AMP amp-lightbox-gallery用法及代碼示例
- Google AMP amp-soundcloud用法及代碼示例
注:本文由純淨天空篩選整理自somsagar2019大神的英文原創作品 Google AMP amp-mustache。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。