amp-list
Introduction
The amp-list
enables client-side rendering in AMP. Content can either be fetched from a JSON endpoint or locally from amp-state
.
Setup
Import the amp-list
component ...
<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
... and the amp-mustache
component in the header.
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
Import the amp-bind
component for dynamically changing the content of an amp-list
.
<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
Basic usage
The amp-list
content is provided by a JSON CORS endpoint, which is defined by the src
attribute. The URL's protocol must be HTTPS. The response must be a JSON object containing an array property items
, for example:
{ "items": [ { "title": "amp-carousel", "url": "https://ampbyexample.com/components/amp-carousel" }, ... ] }
The list content is rendered via an amp-mustache template. The template can be specified either by id, or by using a nested element. For performance reasons, we're adding binding="no"
as we're not using amp-bind
.
<amp-list layout="fixed-height" height="100" src="/static/samples/json/examples.json" binding="no">
<template type="amp-mustache">
<div><a href="{{url}}">{{title}}</a></div>
</template>
</amp-list>
Re-using an existing template
The template can also be specified using an ID of an existing template
element.
<template type="amp-mustache" id="amp-template-id">
<div><a href="{{url}}">{{title}}</a></div>
</template>
<amp-list layout="fixed-height" height="100" src="/static/samples/json/examples.json" template="amp-template-id" binding="no">
</amp-list>
Handling list overflow
If the amp-list
content requires more space than available, the AMP runtime will display the overflow element (if specified).
<amp-list layout="fixed-height" height="48" src="/static/samples/json/examples.json" binding="no">
<div overflow role="button" aria-label="Show more" class="list-overflow">
Show more
</div>
<template type="amp-mustache">
<div><a href="{{url}}">{{title}}</a></div>
</template>
</amp-list>
Dynamic src
The content of an amp-list
can be changed dynamically by modifying it's src
value using amp-bind
.
<amp-list layout="fixed-height" height="100" src="/static/samples/json/examples.json" [src]="srcUrl" binding="no">
<template type="amp-mustache">
<div><a href="{{url}}">{{title}}</a></div>
</template>
</amp-list>
<button on="tap:AMP.setState({ srcUrl: '/static/samples/json/examples2.json' })">Change source</button>
Rendering amp-state
This sample shows off one of amp-list
's most powerful features: being able to directly render objects from amp-state
, This is possible by binding the results of an amp-bind
expression to the src
attribute: [src]="items"
. To make sure that the amp-list
resizes accordingly, we dynamically calculate the height
based on the number of list elements: [height]="items.length * 22"
. The initial state (an empty list) is defined in an amp-state
element.
amp-bind
expressions are not evaluated on page load, but only after an user interaction has taken place. Initial amp-list
content still needs to be fetched from a JSON endpoint. <amp-state id="items">
<script type="application/json">
[]
</script>
</amp-state>
<amp-list layout="fixed-height" height="0" [src]="items" [height]="items.length * 22" single-item items="." binding="no">
<template type="amp-mustache">
<div>{{ . }}</div>
</template>
</amp-list>
<button on="tap:AMP.setState({ items: items.splice(items.length, 0, 'item ' + items.length) })">
Add item
</button>
Empty list content
If the list contains an empty array, you can use mustache inverted sections to print a message.
For this to work, we have to use the single-item
attribute and items
attribute to be able to access the root items
array inside the template.
<amp-list layout="fixed-height" height="100" src="/static/samples/json/examples-empty.json" [src]="emptyListSampleSrc || '/static/samples/json/examples-empty.json'" single-item items="." reset-on-refresh binding="no">
<template type="amp-mustache">
{{#items}}
<div><a href="{{url}}">{{title}}</a></div>
{{/items}}
{{^items}}
<div>No items founds.</div>
{{/items}}
</template>
</amp-list>
<button on="tap:AMP.setState({ emptyListSampleSrc: '/static/samples/json/examples.json' })">
Add items
</button>
Using a placeholder
You can use a custom placeholder that looks similar to the rendered items to improve the user experience while the list is loading. We are using an endpoint which intentionally delays the response by 10 seconds.
<amp-list id="amp-list-placeholder" noloading layout="fixed-height" height="654" src="/documentation/examples/api/slow-json-with-items/?delay=5000" binding="no">
<div placeholder>
<div class="product">
<div class="image-placeholder"></div>
<div>Loading...</div>
</div>
<div class="product">
<div class="image-placeholder"></div>
<div>Loading...</div>
</div>
<div class="product">
<div class="image-placeholder"></div>
<div>Loading...</div>
</div>
</div>
<template type="amp-mustache">
<div class="product">
<amp-img width="150" height="100" alt="{{name}}" src="{{img}}"></amp-img>
<div>
<div>{{name}}</div>
<div>{{{stars}}}</div>
<div>${{price}}</div>
</div>
</div>
</template>
</amp-list>
Triggering refresh
You can use the refresh
action to trigger a refresh on amp-list
. Notice how we are using reset-on-refresh
attribute to ensure that the placeholder is reloaded.
<button on="tap:myAmpList.refresh">Refresh list</button>
<amp-list id="myAmpList" reset-on-refresh layout="fixed-height" height="600" src="/documentation/examples/api/slow-json-with-items/?delay=1000" binding="no">
<template type="amp-mustache">
<div class="product">
<amp-img width="150" height="100" alt="{{name}}" src="{{img}}"></amp-img>
<div>
<div>{{name}}</div>
<div>{{{stars}}}</div>
<div>${{price}}</div>
</div>
</div>
</template>
</amp-list>
Если на этой странице нет ответов на все ваши вопросы, обратитесь к другим пользователям AMP и обсудите свой конкретный сценарий использования.
Перейти на Stack Overflow Не нашли пояснения к функции?Проект AMP активно поощряет ваше участие и сотрудничество! Мы надеемся, что вы станете постоянным участником нашего открытого сообщества, но разовые вклады в работу над задачами, которые вам особенно интересны, также приветствуются.
Редактировать пример на GitHub-
Written by @kul3r4