AMP
  • email

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="https://amp.dev/static/samples/json/examples.json" binding="no">
  <template type="amp-mustache">
    <div><a href="https://amp.dev/%7B%7Burl%7D%7D">{{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="https://amp.dev/%7B%7Burl%7D%7D">{{title}}</a></div>
</template>
<amp-list layout="fixed-height" height="100" src="https://amp.dev/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).

Show more
<amp-list layout="fixed-height" height="48" src="https://amp.dev/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="https://amp.dev/%7B%7Burl%7D%7D">{{title}}</a></div>
  </template>
</amp-list>

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.

Loading...
Loading...
Loading...
<amp-list id="amp-list-placeholder" noloading layout="fixed-height" height="654" src="https://amp.dev/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="https://amp.dev/%7B%7Bimg%7D%7D"></amp-img>
          <div>
            <div>{{name}}</div>
            <div>{{{stars}}}</div>
            <div>${{price}}</div>
          </div>
      </div>
  </template>
</amp-list>
자세한 설명이 필요하신가요?

이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.

Stack Overflow로 이동
설명이 부족한 기능을 발견하셨나요?

AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.

GitHub에서 샘플 수정하기