Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2008-07-20 08:30:51

kkobashi
Member
Registered: 2008-01-27
Posts: 51
Website

Re: Detecting no search results and pager component

Ok I got it working and it wasn’t intuitively obvious at all. Really, this sort of stuff should be used as a tutorial or example in the Textbook documentation to get people off and running. This could save countless hours and headaches.

Feel free to put this in the tutorial section. I guess you can title it “How To Generate A Search Result Page”.

Here is what I did:

1) Create a section
=============
Section name: search
Section title: search
Uses page: search
Uses style: default
Selected by default: no
On front page: no
Syndicate: no
Include in site search: no

2) Create a page template named ‘search’
==========================

Here’s the meat of the search result code. You basically want to create a full fledged html page here and put in the div search snippet below.

(snip)
<div class="search-results">
  <h2>Search Results</h2>
  <form method="get" action="/search/">
    <input type="text" name="q" value="" size="30" /> <input type="submit" value="Search" />
  </form>
  <txp:article pgonly="1" limit="5"/>
  <p><txp:search_result_count/></p>
  <txp:if_search_results>
    <txp:article searchform="jungle-search-results" limit="5"/>       
    <p class="oldnewlinks">
      <txp:older showalways="1">previous</txp:older> | <txp:newer showalways="1">next</txp:newer>
    </p>
  </txp:if_search_results>
</div>
(snip)

The above will display 5 results per page based on the default sorting of “descending score” and put in previous and next pager links. It will also put in the search box for user friendly convenience. There is an important thing to note here. You have to put in a limit for the first article tag else you will not see the prev/next links. It should be the same number as what you wish to show per page. You should be nice to the user and show the number of search results generated. It must be done after the first article tag (which generates statistical count info). Then a check is made to see if there are any search results and if so they are generated.

3) Create your search-result form
=====================

Name: search-result
Type: article

<div class="search-result">
<h3><txp:permlink><txp:search_result_title /></txp:permlink></h3>
  <p><span class="text"><txp:search_result_excerpt hilight="strong" limit="3"/></span><br/><span class="url"><txp:search_result_url/></span></p>
</div>

This form snippet gets invoked repeatedly per search result as <txp:article searchform=“search-results”/> gets called. Here I set things to look like Google’s search results. Only 3 instances will be shown per excerpt and the search term is marked strong.

4) CSS file
Here is the relevant parts of my search.css stylesheet:

h2 {
  color: red;
  font-size: 130%;
}
strong {
  font-weight: bold;
}
.search-results {
  padding: 1em;
}
.search-results form {
  padding-bottom: 1em;
}
.search-results p.oldnewlinks {
  text-align: right;
}
.search-result h3 a {
  font-size: 80%;
  color: black;
  text-decoration: underline;
}
.search-result span.text {
  font-size: 85%;
}
.search-result span.url a {
  font-size: 70%;
  color: green;
}

Thanks to thebombsite, Els, jakob, and gocom for their assistance.

Last edited by kkobashi (2008-07-20 08:43:35)


Kerry Kobashi
Kobashi Computing

Offline

#14 2008-07-20 16:10:19

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Detecting no search results and pager component

Glad you figured it out, I realize now that I forgot to mention this:

It’s important that both article tags have the same attributes (other than pgonly), otherwise the counts won’t match the list displayed by the second tag.

and forgot to point you to the article in question on Threshold State.
When you’ve been using it for such a long time you tend to forget that details like this are not necessarily obvious to others…

Thanks for posting your detailed instructions, lots of new users will benefit from it!

Offline

Board footer

Powered by FluxBB