Skip to content

Instantly share code, notes, and snippets.

@jkersu
Last active March 10, 2020 09:50
Show Gist options
  • Save jkersu/bacb2dcbd04302aca217bf3b1ebd442b to your computer and use it in GitHub Desktop.
Save jkersu/bacb2dcbd04302aca217bf3b1ebd442b to your computer and use it in GitHub Desktop.
Basic Site Search in SilverStripe 4
Sometimes when implementing search functionality on a SilverStripe website, you may find youself just needing a very basic site search without wanting to implement a full-scale search platform like Solr. In that case, SilverStripe 4 by default offers the ability to search Pages and File objects directly out of the box. This functionality is handled in ContentControllerSearchExtension.php which means that any Controllers that extend off ContentController.php can enable the Site Search functionality.
To setup a basic site search, there are only 3 key steps required:
Step 1:
We first want to enable the search engine and this can be done simply by adding the following to mysite/_config.php. By default this will search both Pages and Files objects. If you want to restrict the search to only 1 type, you can also pass Page::class or File::class into the enable() method as a parameter:
FulltextSearchable::enable();
Step 2: Add the Search Form to any template where you want it to appear. Very often this Search Form is typically placed in the template for your Header, i.e. Header.ss. Please also note that the form action is manually hardcoded below so that we guarantee the output of the page. By specifying the page type that handles the search we can ensure that the results would always be rendered the same regardless of which page you are on.
<% if $SearchForm %>
<form id="search" action="home/SearchForm" method="get" enctype="application/x-www-form-urlencoded">
<input class="text" type="text" name="Search" placeholder="Enter keyword">
</form>
<% end_if %>
Step 3: Now we just need to create a new template to render the search results. This usually involves creating a new SilverStripe template called Page_results.ss and placing it in the following directory:
themes/app/templates/Layout/Page_results.ss
<div id="Content" class="searchResults">
<% if $Query %>
<p class="searchQuery"><strong>You searched for &quot;{$Query}&quot;</strong></p>
<% end_if %>
<% if $Results %>
<ul id="SearchResults">
<% loop $Results %>
<li>
<a class="searchResultHeader" href="$Link">
<% if $MenuTitle %>
$MenuTitle
<% else %>
$Title
<% end_if %>
</a>
<p>$Content.LimitWordCountXML</p>
<a class="readMoreLink" href="$Link"
title="Read more about &quot;{$Title}&quot;"
>Read more about &quot;{$Title}&quot;...</a>
</li>
<% end_loop %>
</ul>
<% else %>
<p>Sorry, your search query did not return any results.</p>
<% end_if %>
<% if $Results.MoreThanOnePage %>
<div id="PageNumbers">
<% if $Results.NotLastPage %>
<a class="next" href="$Results.NextLink" title="View the next page">Next</a>
<% end_if %>
<% if $Results.NotFirstPage %>
<a class="prev" href="$Results.PrevLink" title="View the previous page">Prev</a>
<% end_if %>
<span>
<% loop $Results.Pages %>
<% if $CurrentBool %>
$PageNum
<% else %>
<a href="$Link" title="View page number $PageNum">$PageNum</a>
<% end_if %>
<% end_loop %>
</span>
<p>Page $Results.CurrentPage of $Results.TotalPages</p>
</div>
<% end_if %>
</div>
And if you run a dev/build?flush=all now, you should have a fully functioning basic site search straight out of the box. Happy searching :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment