Chapter 10: Search
Adding Alternate Search Templates
Problem
You want to use an alternate design or layout for the search results returned by Movable Type's public search engine.
Solution
Create the template and use the AltTemplate configuration
directive to define its use.
Discussion
Typically, when a search is performed, the default template,
search_templates/default.tmpl, is rendered to display the
results. The built-in search can perform a comment search in which the
template search_templates/comments.tmpl is used.
However, if you have multiple weblogs that employ the search functionality, you may very well want the search results from one weblog to look different from that of another. To do this, you can use alternate search templates.
Create the template file
For each different look-and-feel that you want, create a search results template file, and put them in your `search_templates/ directory.
Usually, the best thing to do if you're starting out is to make a copy of
search_templates/default.tmplcalled, for example,search_templates/my_customized_search.tmpland use that as a starting point. Of course, the name of the template is totally up to you and your visitors will never see it.Tell Movable Type about it
Open up your
mt-config.cgifile in a text editor, and use theAltTemplateconfiguration directive to tell Movable Type about your new search template and the template file to use in rendering it.For example, if you have two weblogs, "Work," and "Play," and you want each weblog to have its own search results template, you might add something like this:
AltTemplate work work.tmpl
AltTemplate play play.tmplThese two lines tell Movable Type that when the
worktemplate is requested in a search query, it should use thework.tmpltemplate file to display the results and that when theplaytemplate is requested, it should use theplay.tmpltemplate file.Search, customize, and reload
At this point, you're probably going to want to customize your new template before unleashing it on the world.
To do so, take the following URL:
http://www.example.com/path/to/mt-search.cgi?Template=NAME...and substitute the placehoders above with the:
- correct domain and path to
mt-search.cgifor your installation - template name you chose in the previous step for
NAME.
When the page loads, it should be showing your template, which you can edit and reload as many times as you like without ever affecting your visitors. If you've edited your blog templates before, the search templates will feel very familiar to you except that, in addition many tags you're used to, they also use the special search template tags.
Once you have it just right, move on to step 4.
Notes: If you get an error like "No alternate template is specified for the Template 'work'", check the
AltTemplatesetting in yourmt-config.cgifile and make sure that both the template name is identical to the one used in the URL and that that the template filename is identical to the file insearch_templatesfolder.- correct domain and path to
Configure the search form
Now that your search template is perfect, it's time to let the world use it in their searches. If you don't already have a search form on, for example, on your main index, see "Creating a search form".
To use your new template in your search form, specify it a hidden form input. Anywhere between the
<form ...>and</form>tags, add the following substituting your template name for NAME:<input type="hidden" name="Template" value="NAME" />If you wish to restrict the search to just this one weblog, you may also
want to add a hidden IncludeBlogs paramter with the blog ID like so:<input type="hidden" name="IncludeBlogs" value="<$MTBlogID$>" />



