search-result.tpl.php

You are here

  1. 7 modules/search/search-result.tpl.php
  2. 6 modules/search/search-result.tpl.php

Default theme implementation for displaying a single search result.

This template renders a single search result and is collected into search-results.tpl.php. This and the parent template are dependent to one another sharing the markup for definition lists.

Available variables:

  • $url: URL of the result.
  • $title: Title of the result.
  • $snippet: A small preview of the result. Does not apply to user searches.
  • $info: String of all the meta information ready for print. Does not apply to user searches.
  • $info_split: Contains same data as $info, split into a keyed array.
  • $module: The machine-readable name of the module (tab) being searched, such as "node" or "user".
  • $title_prefix (array): An array containing additional output populated by modules, intended to be displayed in front of the main title tag that appears in the template.
  • $title_suffix (array): An array containing additional output populated by modules, intended to be displayed after the main title tag that appears in the template.

Default keys within $info_split:

  • $info_split['module']: The module that implemented the search query.
  • $info_split['user']: Author of the node linked to users profile. Depends on permission.
  • $info_split['date']: Last update of the node. Short formatted.
  • $info_split['comment']: Number of comments output as "% comments", % being the count. Depends on comment.module.

Other variables:

  • $classes_array: Array of HTML class attribute values. It is flattened into a string within the variable $classes.
  • $title_attributes_array: Array of HTML attributes for the title. It is flattened into a string within the variable $title_attributes.
  • $content_attributes_array: Array of HTML attributes for the content. It is flattened into a string within the variable $content_attributes.

Since $info_split is keyed, a direct print of the item is possible. This array does not apply to user searches so it is recommended to check for its existence before printing. The default keys of 'type', 'user' and 'date' always exist for node searches. Modules may provide other data.

<?php
  php if (isset($info_split['comment'])): ?>
    <span class="info-comment">
      <?php print $info_split['comment']; ?>
    </span>
  <?php endif; ?>
?>

To check for all available data within $info_split, use the code below.

<?php
  php print '<pre>'. check_plain(print_r($info_split, 1)) .'</pre>'; ?>
?>

See also

template_preprocess()

template_preprocess_search_result()

template_process()

1 theme call to search-result.tpl.php

File

modules/search/search-result.tpl.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Default theme implementation for displaying a single search result.
  5. *
  6. * This template renders a single search result and is collected into
  7. * search-results.tpl.php. This and the parent template are
  8. * dependent to one another sharing the markup for definition lists.
  9. *
  10. * Available variables:
  11. * - $url: URL of the result.
  12. * - $title: Title of the result.
  13. * - $snippet: A small preview of the result. Does not apply to user searches.
  14. * - $info: String of all the meta information ready for print. Does not apply
  15. * to user searches.
  16. * - $info_split: Contains same data as $info, split into a keyed array.
  17. * - $module: The machine-readable name of the module (tab) being searched, such
  18. * as "node" or "user".
  19. * - $title_prefix (array): An array containing additional output populated by
  20. * modules, intended to be displayed in front of the main title tag that
  21. * appears in the template.
  22. * - $title_suffix (array): An array containing additional output populated by
  23. * modules, intended to be displayed after the main title tag that appears in
  24. * the template.
  25. *
  26. * Default keys within $info_split:
  27. * - $info_split['module']: The module that implemented the search query.
  28. * - $info_split['user']: Author of the node linked to users profile. Depends
  29. * on permission.
  30. * - $info_split['date']: Last update of the node. Short formatted.
  31. * - $info_split['comment']: Number of comments output as "% comments", %
  32. * being the count. Depends on comment.module.
  33. *
  34. * Other variables:
  35. * - $classes_array: Array of HTML class attribute values. It is flattened
  36. * into a string within the variable $classes.
  37. * - $title_attributes_array: Array of HTML attributes for the title. It is
  38. * flattened into a string within the variable $title_attributes.
  39. * - $content_attributes_array: Array of HTML attributes for the content. It is
  40. * flattened into a string within the variable $content_attributes.
  41. *
  42. * Since $info_split is keyed, a direct print of the item is possible.
  43. * This array does not apply to user searches so it is recommended to check
  44. * for its existence before printing. The default keys of 'type', 'user' and
  45. * 'date' always exist for node searches. Modules may provide other data.
  46. * @code
  47. * <?php if (isset($info_split['comment'])): ?>
  48. * <span class="info-comment">
  49. * <?php print $info_split['comment']; ?>
  50. * </span>
  51. * <?php endif; ?>
  52. * @endcode
  53. *
  54. * To check for all available data within $info_split, use the code below.
  55. * @code
  56. * <?php print '<pre>'. check_plain(print_r($info_split, 1)) .'</pre>'; ?>
  57. * @endcode
  58. *
  59. * @see template_preprocess()
  60. * @see template_preprocess_search_result()
  61. * @see template_process()
  62. *
  63. * @ingroup themeable
  64. */
  65. ?>
  66. <li class="<?php print $classes; ?>"<?php print $attributes; ?>>
  67. <?php print render($title_prefix); ?>
  68. <h3 class="title"<?php print $title_attributes; ?>>
  69. <a href="<?php print $url; ?>"><?php print $title; ?></a>
  70. </h3>
  71. <?php print render($title_suffix); ?>
  72. <div class="search-snippet-info">
  73. <?php if ($snippet): ?>
  74. <p class="search-snippet"<?php print $content_attributes; ?>><?php print $snippet; ?></p>
  75. <?php endif; ?>
  76. <?php if ($info): ?>
  77. <p class="search-info"><?php print $info; ?></p>
  78. <?php endif; ?>
  79. </div>
  80. </li>

Related topics

Comments

There's also another variable, "$result", available in the template.

This contains the full node, in case you want to print out just the node body or teaser instead of the "snippet".

But wont this result in printing "array" instead of the node body?

I meant that you can choose from a number of elements in the $result array (node body, node title, etc), to display only what you want, instead of the predefined snippet.

So I'm having a really odd, really frustrating issue.

When I print the $title, or even $result['title'], or any other piece of content for a search result nothing displays. So:

When I do this:

<?php
echo $title;
?>

There is nothing displayed...a blank page.

BUT when I do this:

<?php
echo $title;exit;
?>

The node title IS displayed on the exited-out page.

Any element from the $result array or any of the listed available variables do the same thing. I'm getting blank search results if I don't exit out the page.

Even when I use the code snippet available here: http://drupal.org/node/254940#search-result-headings
I get nothing. Every search result comes back as blank. Inspecting the code after it renders shows nothing there, so it isn't a CSS issue with making teh text white or anything.

Any ideas?

If it is a news item, say, that we are looking for it's important to have the created date as an option here as well.

as mentioned earlier, you can access the whole node object in $result, including creation date, so adding something like this:

<?php
print format_date($result['node']->created,"custom","j. F");
?>

will output+format a date. See format_date for more infos on outputting dates.

$info_split['type'] is not available on my system.
it is not set (or reset) in search.pages.inc::template_preprocess_search_result ...

$result['type'] works fine though.