News Article
Introduction
This is a sample template for a news article in AMP. It demonstrates the usage of AMP components which works well in news articles. Examples include social sharing, image galleries, personalized content, ads, and more.
Setup
All additionally used AMP components must be imported in the header. Import amp-social-share
for adding share buttons
<script async custom-element="amp-social-share" src="https://cdn.ampproject.org/v0/amp-social-share-0.1.js"></script>
Import amp-iframe
to embed an interactive chart
<script async custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"></script>
Import amp-carousel
to implement an image gallery
<script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script>
Import amp-user-notification
to display a cookie notification
<script async custom-element="amp-user-notification" src="https://cdn.ampproject.org/v0/amp-user-notification-0.1.js"></script>
Import amp-list
to get a fresh a list of related articles
<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
Import amp-mustache
as a format template for amp-list
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
Import amp-analytics
for tracking usage
<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
Import amp-ad
to display ads
<script async custom-element="amp-ad" src="https://cdn.ampproject.org/v0/amp-ad-0.1.js"></script>
Metadata
The Top Stories carousel requires schema.org markup for one of the following types: Article, NewsArticle, BlogPosting, or VideoObject. Learn more.
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "NewsArticle",
"mainEntityOfPage": "https://amp.dev/samples_templates/news_article/",
"headline": "Lorem Ipsum",
"datePublished": "2016-04-21T11:55:02Z",
"dateModified": "2016-04-21T11:55:02Z",
"description": "A sample news article build with AMP.",
"author": {
"@type": "Person",
"name": "Sebastian Benz"
},
"publisher": {
"@type": "Organization",
"name": "Google",
"logo": {
"@type": "ImageObject",
"url": "http://cdn.ampproject.org/logo.jpg",
"width": 600,
"height": 60
}
},
"image": {
"@type": "ImageObject",
"url": "/static/samples/img/landscape_lake_1280x857.jpg",
"height": 1280,
"width": 857
}
}
</script>
Social sharing
The Social Share extension provides a common interface for share buttons. Learn more about amp-social-share
here.
<p class="heading">
<amp-social-share type="twitter" width="45" height="33"></amp-social-share>
<amp-social-share type="facebook" width="45" height="33" data-attribution="254325784911610"></amp-social-share>
<amp-social-share type="gplus" width="45" height="33"></amp-social-share>
<amp-social-share type="email" width="45" height="33"></amp-social-share>
<amp-social-share type="pinterest" width="45" height="33"></amp-social-share>
</p>
Ad support
There are no limitations regarding the number or placement of ads in an AMP. Learn more about amp-ad
here.
<div class="amp-ad-container">
<amp-ad width="300" height="250" type="a9" data-aax_size="300x250" data-aax_pubname="test123" data-aax_src="302">
</amp-ad>
</div>
Image galleries
Use amp-carousel
for image galleries. Learn more about creating image galleries in AMP here.
<amp-carousel width="1280" height="1000" layout="responsive" type="slides">
<figure class="figure">
<amp-img src="/static/samples/img/landscape_lake_1280x857.jpg" width="1280" height="857" layout="responsive"></amp-img>
<figcaption>Each image has a different caption.</figcaption>
</figure>
<figure class="figure">
<amp-img src="/static/samples/img/landscape_village_1280x853.jpg" width="1280" height="853" layout="responsive"></amp-img>
<figcaption>This caption is different.</figcaption>
</figure>
<figure class="figure">
<amp-img src="/static/samples/img/landscape_desert_1280x853.jpg" width="1280" height="853" layout="responsive"></amp-img>
<figcaption>The third image has its caption.</figcaption>
</figure>
</amp-carousel>
Interactive content
To achieve its performance, AMP HTML doesn't allow custom Javascript. This doesn't mean that you can't created visually rich and interactive websites with AMP. Embed interactive content via amp-iframe
. Learn more about amp-iframe
here.
<figure class="figure">
<amp-iframe src="https://www.google.us/trends/embed/US_cu_82I1CVMBAACV6M_en/horserace_chart_3c6cdb21-e1eb-4412-9c28-707c3638ea35?hl=en&template=fe" title="Interactive chart displaying 2016 Oscar Best Picture search interest by date" height="360" layout="fixed-height" frameborder="0" sandbox="allow-scripts allow-same-origin">
<amp-img src="/static/samples/img/oscars_placeholder_1.png" layout="fixed-height" height="360" width="auto" placeholder>
</amp-img>
</amp-iframe>
<figcaption>An interactive chart - in AMP!</figcaption>
</figure>
Embedding related content
It's a good idea to recommend related content to your readers. With amp-list
it is possible to dynamically include personalized content into your AMPs. It's a good idea to link to the AMP version of related content in order to keep the user in the fast AMP world. Learn more about amp-list
here
<amp-list width="300" height="75" layout="responsive" src="/static/samples/json/related_articles.json" binding="no">
<template type="amp-mustache">
<a class="related" href="{{url}}"><amp-img width="101" height="75" src="{{thumbnail}}"></amp-img><span>{{title}}</span></a>
</template>
</amp-list>
Cookie consent
Use amp-user-notification
to implement a cookie consent form (if needed). By default, the AMP runtime doesn't use cookies. Some analytics vendors might use require cookies though. Learn more about amp-user-notification
here
<amp-user-notification class="cookie-disclaimer" layout="nodisplay" id="amp-user-notification1">
This page might use cookies if your analytics vendor requires them.
<button on="tap:amp-user-notification1.dismiss">Accept</button>
</amp-user-notification>
User analytics
Analytics must be configured in the body. Here we use Google Analytics to track pageviews.
<amp-analytics type="googleanalytics">
<script type="application/json">
{
"vars": {
"account": "UA-73836974-1"
},
"triggers": {
"default pageview": {
"on": "visible",
"request": "pageview",
"vars": {
"title": "{{title}}"
}
}
}
}
</script>
</amp-analytics>
Se as explicações nesta página não respondem a todas as suas perguntas, entre em contato com outros usuários de AMP para discutir seu caso de uso específico.
Ir para o Stack Overflow Falta explicar algum recurso?O projeto AMP incentiva fortemente sua participação e contribuições! Esperamos que você se torne um participante assíduo de nossa comunidade de código aberto, mas também agradecemos contribuições pontuais para problemas que você tenha particular interesse.
Editar amostra no GitHub-
Written by @aghassemi