# Tutorial 4 - Site Search ## Overview This is a short tutorial demonstrating how to add search functionality to a SilverStripe site. It is recommended that you have completed the earlier tutorials, especially the tutorial on forms, before attempting this tutorial. While this tutorial will add search functionality to the site built in the previous tutorials, it should be straight forward to follow this tutorial on any site of your own. ## What are we working towards? We are going to add a search box on the top of the page. When a user types something in the box, they are taken to a results page. ![](_images/searchresults-small.jpg) ## Creating the search form To enable the search engine you need to include the following code in your `mysite/_config.php` file. This will enable fulltext search on page content as well as names of all files in the `/assets` folder. :::php FulltextSearchable::enable(); After including that in your `_config.php` you will need to rebuild the database by visiting `http://yoursite.com/dev/build` in your web browser. This will add the fulltext search columns. The actual search form code is already provided in FulltextSearchable so when you add the enable line above to your `_config.php` you can add your form as `$SearchForm`. ### Custom CSS code Add the following css code to the *themes/tutorial/css/layout.css* file. This will style the header form and search results page. :::css #Header form { float:right; width:160px; margin:25px 25px 0px 25px; } #Header form * { display:inline !important; } #Header form div { } #Header form input.text { width:110px; color:#000; background:#f0f0f0; border:1px solid #aaa; padding:3px; } #Header form input.action { font-weight:bold; } .searchResults h2 { font-size:2.2em; font-weight:normal; color:#0083C8; margin-bottom:15px; } .searchResults p.searchQuery { color:#333; margin-bottom:10px; } .searchResults ul#SearchResults li { margin-bottom:20px; } ul#SearchResults p { font-size:1.1em; font-weight:normal; line-height:2em; color:#333; } ul#SearchResults a.searchResultHeader { font-size:1.3em; font-weight:bold; color:#0083C8; text-decoration:none; margin:20px 0 8px 0; padding-left:20px; background:url(../images/treeicons/search-file.gif) no-repeat left center; } ul#SearchResults a { text-decoration:none; color:#0083C8; } ul#SearchResults a:hover { border-bottom:1px dotted #0083C8; } ## Adding the search form We then just need to add the search form to the template. Add *$SearchForm* to the 'Header' div in *themes/tutorial/templates/Page.ss*. *themes/tutorial/templates/Page.ss* :::ss
You searched for "{$Query}"
<% end_if %> <% if Results %>$Content.LimitWordCountXML
Read more about "{$Title}"...Sorry, your search query did not return any results.
<% end_if %> <% if Results.MoreThanOnePage %> <% end_if %>