-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsearch.php
More file actions
100 lines (90 loc) · 4.06 KB
/
search.php
File metadata and controls
100 lines (90 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Search Template
*
* This template handles the search page. The search route is registered
* in theme.php using the hook system to intercept /search requests.
*
* Available variables (passed from theme.php):
* $query - Pre-configured Query with search applied
* $searchQuery - The user's search term (string)
* $request - The HTTP request object
* $ava - Template helper
* $site - Site configuration array
*
* @see https://ava.addy.zone/docs/theming
* @see https://ava.addy.zone/docs/api
*/
$pageTitle = 'Search' . ($searchQuery ? ': ' . $searchQuery : '') . ' - ' . $site['name'];
?>
<?= $ava->partial('header', ['request' => $request, 'pageTitle' => $pageTitle]) ?>
<div class="container">
<header class="page-header">
<h1>Search</h1>
</header>
<?php /* Search form - uses GET method so results are bookmarkable */ ?>
<form class="search-form" action="/search" method="get">
<input
type="search"
name="q"
class="search-input"
placeholder="Search content..."
value="<?= $ava->e($searchQuery) ?>"
autofocus
>
<button type="submit" class="btn btn-primary">Search</button>
</form>
<?php if ($searchQuery !== ''): ?>
<?php
/**
* Query Execution & Counting
*
* $query->get() returns the paginated results.
* $query->count() returns the total number of matches
* (not just the current page).
*/
$results = $query->get();
$total = $query->count();
?>
<p class="search-results-info">
Found <?= $total ?> result<?= $total !== 1 ? 's' : '' ?> for "<?= $ava->e($searchQuery) ?>"
</p>
<?php if (empty($results)): ?>
<div class="search-empty">
<p>No results found. Try a different search term.</p>
</div>
<?php else: ?>
<div class="archive-list">
<?php foreach ($results as $entry): ?>
<article class="archive-item">
<h2>
<a href="<?= $ava->url($entry->type(), $entry->slug()) ?>">
<?= $ava->e($entry->title()) ?>
</a>
</h2>
<div class="meta">
<?php /* Show content type so users know what they're clicking */ ?>
<span><?= $ava->e(ucfirst($entry->type())) ?></span>
<?php if ($entry->date()): ?>
·
<time datetime="<?= $entry->date()->format('c') ?>">
<?= $ava->date($entry->date()) ?>
</time>
<?php endif; ?>
</div>
<?php if ($entry->excerpt()): ?>
<p class="excerpt"><?= $ava->e($entry->excerpt()) ?></p>
<?php endif; ?>
</article>
<?php endforeach; ?>
</div>
<?php /* Preserve search query in pagination URLs */ ?>
<?= $ava->pagination($query, '/search?q=' . urlencode($searchQuery)) ?>
<?php endif; ?>
<?php else: ?>
<div class="search-empty">
<p>Enter a search term above to find content.</p>
</div>
<?php endif; ?>
</div>
<?= $ava->partial('footer') ?>