amp-render
Introduction
The amp-render
component provides a simple way to load JSON from a server and render it using a mustache template.
Setup
First, you need to import the script for the amp-render
extension.
<script async custom-element="amp-render" src="https://cdn.ampproject.org/v0/amp-render-1.0.js"></script>
When you use amp-render
, you'll almost certainly also want to use amp-mustache
.
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
In the final example on this page, we'll also be using amp-script
, so we'll import that extension's script as well.
<script async custom-element="amp-script" src="https://cdn.ampproject.org/v0/amp-script-0.1.js"></script>
Basic example
Here's a basic example, applying a simple template to a simple JSON object. The JSON looks like this:
{ "country": "Nigeria", "city": "Lagos" }
Here's the <amp-render>
and the <template>
:
<amp-render class="sample" src="/static/samples/json/cities-lagos.json" layout="fixed-height" height="60">
<template type="amp-mustache">
<div class="line">{{city}} is a city in {{country}}.</div>
</template>
</amp-render>
Iterating through a list
In this example, we drill down to access a sub-object of some more elaborate JSON. Our template then iterates through elements of the array that sub-object contains.
<amp-render class="sample" src="/static/samples/json/cities.json" layout="fixed-height" height="105">
<template type="amp-mustache">
{{#planets}}
{{#earth}}
{{#continents}}
{{#africa}}
<div class="line">{{city}} is a city in {{country}}.</div>
{{/africa}}
{{/continents}}
{{/earth}}
{{/planets}}
</template>
</amp-render>
Using more attributes
Now, let's try some more attributes.
-
binding:
binding="never"
tells AMP there's no need to evaluateamp-bind
bindings in this template. This is more efficient, especially as our template contains no bindings. -
key: this allows us to choose a child of the JSON object, and apply that to the template instead of the entire object.
-
template: this lets us specify a template that's not a child of the
amp-render
.
<amp-render class="sample" src="/static/samples/json/cities.json" layout="fixed-height" height="105" binding="never" key="planets.earth.continents" template="cities-countries">
</amp-render>
<template id="cities-countries" type="amp-mustache">
{{#africa}}
<div class="line">{{city}} is a city in {{country}}.</div>
{{/africa}}
</template>
Using amp-script
amp-render
can also use amp-script
as a data source. In this example, instead of using the key
attribute, we fetch the city data with our own JavaScript, then extract the desired sub-object.
To learn more about fetching data with amp-script
, see this amp-script
example.
<amp-render class="sample" src="amp-script:dataFunctions.fetchData" layout="fixed-height" height="52">
<template type="amp-mustache">
{{#southAmerica}}
<div class="line">{{city}} is a city in {{country}}.</div>
{{/southAmerica}}
</template>
</amp-render>
<amp-script id="dataFunctions" script="fetch-data-script" nodom></amp-script>
<script id="fetch-data-script" type="text/plain" target="amp-script">
function fetchData(index) {
return fetch('https://amp.dev/static/samples/json/cities.json')
.then(resp => resp.json())
.then(findContinentsData)
}
function findContinentsData(json) {
return json.planets.earth.continents;
}
exportFunction('fetchData', fetchData);
</script>
이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.
Stack Overflow로 이동 설명이 부족한 기능을 발견하셨나요?AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.
GitHub에서 샘플 수정하기-
Written by @morss