Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to auto-suggest rich content while searching in Google AMP?
To implement auto-suggestion of rich content while typing in an input field, we use the amp-autocomplete component from Google AMP. This component provides dynamic suggestions as users type, enhancing the search experience.
Required Scripts
To create auto-suggest functionality in Google AMP, you need to include these scripts
Installation: Add these script tags to your HTML <head> section to enable AMP autocomplete functionality.
<script async custom-element="amp-autocomplete" src="https://cdn.ampproject.org/v0/amp-autocomplete-0.1.js"></script> <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script> <script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script> <script async src="https://cdn.ampproject.org/v0.js"></script>
Basic Syntax
<amp-autocomplete filter="token-prefix" min-characters="1">
<input type="search" name="query" />
<script type="application/json">
{"items": [...]}
</script>
<template type="amp-mustache">
{{suggestion_template}}
</template>
</amp-autocomplete>
Example: Rich Content Auto-Suggestions
Here's how to implement auto-suggestions with rich content including names and countries
<!DOCTYPE html>
<html ?>
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-autocomplete" src="https://cdn.ampproject.org/v0/amp-autocomplete-0.1.js"></script>
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-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}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-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>
<title>AMP Autocomplete Example</title>
<style amp-custom>
.search-container {
max-width: 400px;
margin: 20px auto;
padding: 20px;
}
input[type="search"] {
width: 100%;
padding: 10px;
font-size: 16px;
border: 2px solid #ddd;
border-radius: 4px;
}
.suggestion-item {
padding: 10px;
border-bottom: 1px solid #eee;
cursor: pointer;
}
</style>
</head>
<body>
<div class="search-container">
<form>
<label>
<span>Search for people:</span>
<amp-autocomplete
filter="token-prefix"
filter-value="name"
min-characters="0">
<input type="search" name="query" placeholder="Type to search..." />
<script type="application/json">
{
"items": [
{"name": "Luffy", "country": "India"},
{"name": "Nami", "country": "USA"},
{"name": "Zoro", "country": "Canada"},
{"name": "Sanji", "country": "France"},
{"name": "Robin", "country": "Japan"}
]
}
</script>
<template type="amp-mustache">
<div class="suggestion-item" data-value="{{name}}, {{country}}">
<strong>{{name}}</strong> - {{country}}
</div>
</template>
</amp-autocomplete>
</label>
</form>
</div>
</body>
</html>
A search input field appears with autocomplete functionality. When you start typing (e.g., "L" for Luffy), rich suggestions show both the name and country in a formatted dropdown list.
Key Attributes
| Attribute | Description |
|---|---|
filter |
Defines how suggestions are filtered (token-prefix, substring, etc.) |
filter-value |
Specifies which JSON property to filter by |
min-characters |
Minimum characters before showing suggestions |
data-value |
Value to populate the input when suggestion is selected |
Conclusion
The amp-autocomplete component provides powerful auto-suggestion capabilities for AMP pages. By combining JSON data with Mustache templates, you can create rich, interactive search experiences that enhance user engagement.
