구글 AMP – List

4 min read

Amp-list는 CORS json 엔드 포인트를 호출하고 템플릿 내부의 json 파일 형식으로 데이터를 표시하는 amp 구성 요소입니다. 예제의 도움으로 이것을 이해합시다.

amp-list를 사용하려면 다음 스크립트를 포함해야 합니다.

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

다음은 amp-list 태그의 형식입니다.

<amp-list width="auto" height="100" layout="fixed-height" src="amplist.json" class="m1">
  <template type="amp-mustache">
    <div class="images_for_display">
      <amp-img width="150"
               height="100"
               alt="{{title}}"
               src="{{url}}">
      </amp-img> 
    </div>
  </template>
</amp-list>

amp-list에 사용되는 src는 나열할 세부 정보가 있는 json 파일입니다. amp-list 내부의 일반 html 태그 또는 amp 구성 요소를 사용하여 json 파일의 데이터를 표시할 수 있습니다. 템플릿 유형 amp-mustache는 표시할 데이터를 데이터 바인딩하는데 사용됩니다.

아래와 같이 실제 예제의 도움으로 이것을 이해합시다.

예제

<!doctype html>
<html ⚡ lang="ko">
<head>
  <meta charset="utf-8">
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <title>Google AMP - Amp List</title>
  <link rel="canonical" href="https://googleblogamp.blogspot.com/2021/01/google-amp-list.html">
  <meta name="viewport" content="width=device-width,minimum-scale=1">
  <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>
  <script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
  <script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
  <style amp-custom>
    amp-img {
      border: 1px solid #ddd;
      border-radius: 4px;
      padding: 5px;
    }
  </style>
</head>
<body>
<h3>Google AMP - Amp List</h3>
<amp-list width="auto" height="100" layout="fixed-height" src="amplist.json" class="m1">
  <template type="amp-mustache">
    <div class="images_for_display">
      <amp-img width="150"
               height="100"
               alt="{{title}}"
               src="{{url}}">
      </amp-img> 
    </div>
  </template>
</amp-list>
</body>
</html>

결과

위에 표시된 작업 예제의 결과는 다음과 같습니다.

위의 예제에서 사용된 json 파일은 다음과 같습니다.

amplist.json

{
  "items": [
    {
      "title": "Christmas Image 1",
      "url": "images/christmas1.jpg"
    },
    {
      "title": "Christmas Image 2",
      "url": "images/christmas2.jpg"
    },
    {
      "title": "Christmas Image 3",
      "url": "images/christmas3.jpg"
    },
    { 
      "title": "Christmas Image 4",
      "url": "images/christmas4.jpg"
    }
  ]
}

아래 코드와 같이 amp-list의 이벤트를 사용하여 목록을 새로고침할 수 있습니다.

예제

<!doctype html>
<html ⚡ lang="ko">
<head>
  <meta charset="utf-8">
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <title>Google AMP - Amp List</title>
  <link rel="canonical" href="https://googleblogamp.blogspot.com/2021/01/google-amp-list.html">
  <meta name="viewport" content="width=device-width,minimum-scale=1">
  <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>
  <script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
  <script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
  <style amp-custom>
    amp-img {
      border: 1px solid #ddd;
      border-radius: 4px;
      padding: 5px;
    }
    button {
      background-color: #ACAD5C;
      color: white;
      padding: 12px 20px;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      float: right;
    }
  </style>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
</head>
<body>
<h3>Google AMP - Amp List</h3>
<button on="tap:amplist.refresh">Refresh Images</button> 
<amp-list id="amplist" width="auto" height="100" layout="fixed-height" src="amplist.json" class="m1">
  <template type="amp-mustache">
    <div class="images_for_display">
      <amp-img width="150"
               height="100"
               alt="{{title}}"
               src="{{url}}">
      </amp-img> 
    </div>
  </template>
</amp-list>
</body>
</html>

위에 제공된 예제의 출력은 다음과 같습니다.

클릭하면 아래와 같이 on 이벤트를 사용하여 새로고침 동작을 호출하는 버튼이 추가되었습니다.

<button on="tap:amplist.refresh">Refresh Images</button>
//amplist is the id used for amp-list

버튼을 클릭하면 json 파일이 다시 호출되고 내용이 로드됩니다. 이미 로드된 이미지가 있는 경우 캐시됩니다.

방문해주셔서 감사합니다. 즐거운 하루 되세요!

관심 있을 만한 글

  • AMP Datepicker는 사용자가 날짜를 선택할 수 있는 페이지에 달력을 표시하는 amp 구성 요소입니다. AMP datepicker는 정적 달력처럼 표시되거나 입력 선택에 따라 버튼 클릭으로 표시될 수 있습니다. amp-date-picker를 작동시키려면 페이지에 다음 스크립트를 추가해야 합니다.…
  • 버튼은 AMP의 또 다른 기능입니다. AMP의 버튼에는 변경 사항이 없으며 표준 HTML 버튼 태그처럼 사용됩니다. AMP 페이지의 버튼과 유일한 차이점은 이벤트가 작동한다는 것입니다. 이 글에서는 버튼의 작동과 AMP 구성 요소와 함께 사용하는 방법을 보여주는 몇 가지 예를 살펴 보겠습니다. …
  • Amp 태그 amp-fit-text는 디스플레이를 렌더링하기에 공간이 충분하지 않은 경우 글꼴 크기를 줄입니다. 이 글에서는 이 태그에 대해 자세히 설명합니다. amp-fit-text가 작동하도록 하려면 다음 스크립트를 추가해야 합니다. <script async custom-element="am…
  • Timeago는 'x'시간 전과 같이 과거와 비교하여 타임 스탬프 세부 정보를 제공합니다. 이 글에서는 이 기능에 대해 자세히 설명하겠습니다. 이 기능을 작업에 삽입하려면 아래의 스크립트를 .html 페이지에 추가해야 합니다. <script async custom-element="amp-…
  • Amp Date countdown이라는 또 다른 amp 구성 요소는 주어진 날짜까지의 일, 시간, 분, 초를 표시하는 데 사용됩니다. 날짜 표시는 선택한 로케일에 따라 수행될 수 있습니다. 기본값은 en (영어)입니다. Amp-date-countdown은 데이터 렌더링에 amp-mustache 템플릿을 사…
  • MathML을 사용하여 수학 공식을 표시할 수 있습니다. 이 글에서는 MathML을 사용하는 방법과 몇 가지 수학 공식을 사용하여 동일한 결과를 표시하는 방법에 대한 실제 예제를 살펴보겠습니다. MathML을 사용하려면 다음 자바스크립트 파일을 포함해야 합니다. <script async cu…

댓글 쓰기