Search templates

Currently I’m looking for a better search template. I’m using

<?php snippet('header') ?>
<ul>
	<?php foreach($results as $result): ?>
		<li>
			<a href="<?php echo $result->url() ?>">
				<?php echo $result->title()->html() ?>
			</a>
		</li>
	<?php endforeach ?>
</ul>

<?php snippet('footer') ?>
```

The results are too simple. I've done research and I can't seem to find any other templates that show more content then just a link to the a page. 

does anybody have a better results page coded up they would be willing to share. 


Thanks


Brandon

Well, you can output whatever fields you have in the page, like a date, some intro text or whatever … you are not limited to just the title.

Any known resource for this. or existing templates?

I don’t have an example and it depends on what fields you have and want to output. It’s no different than any other template, though. I’m not quite sure what you are looking for.

Just found this. It’s pretty nice. I’m going to make some changes but its a good starting point.

<div class="grid">
	<div class="row">

	</div>
	<?php if($results): ?>
		<?php foreach($results as $result): ?>
			<div class="row">
				<div class="slot-6-7-8-9">
					<h1 class="article-title"><a href="<?php echo $result->url() ?>"><?php echo $result->title() ?></a></h1>
					<p>
						<?php echo $result->description() ?> <?php echo excerpt($result->text(), 300) ?> [...] <a href="<?php echo $result->url() ?>">Read more.</a>
					</p>
				</div>
			</div>
		<?php endforeach ?>
		<div class="row">
			<div class="slot-6-7-8-9">
				<?php snippet('pagination', array('pagination' => $results->pagination())) ?>
			</div>
		</div>
	<?php elseif($search->query()): ?>
		<div class="row">
			<div class="slot-6-7-8-9">
				<h3>No posts found matching «<?php echo html($search->query()) ?>».</h3>
				<p>Your search for <strong><?php echo html($search->query()) ?></strong> returned no results.</p>
			</div>
		</div>
	<?php endif ?>
	<div class="row">
		<div class="slot-6-7-8-9">
			<h3>Tags</h3>
			<p>
				<?php foreach($tags as $tag): ?>
					<a href="<?php echo $tag->url() ?>">#<?php echo $tag->name() ?></a>
				<?php endforeach ?>
			</p>
		</div>
	</div>
</div>

Sometimes it’s nice to add a breadcrumb to the search result.

<?php foreach($results as $result): ?>
  ...
  <?php 
    $breadcrumb = '';
    foreach($result->parents()->flip() as $parent) {
      $breadcrumb .= '» ' . $parent->title()->html();
    }
    echo $breadcrumb;
  ?>
  ...
<?php endforeach ?>

Edit: I added the missing echo :slight_smile:

should it look like this

	foreach($result->parents()->flip() as $parent) {
					echo $breadcrumb .= '» ' . $parent->title()->html();
				}

I was wondering if my echo is in the right place though.

I think it’s not because it’s giving me multiple echo’s like this.

				$breadcrumb = '';
				foreach($result->parents()->flip() as $parent) {
				echo  $parent->title()->html(). ' » ';

				}

This is what worked for me.

The alternative would be:

<?php foreach($results as $result): ?>
  ...
  <?php 
    $breadcrumb = '';
    foreach($result->parents()->flip() as $parent) {
      $breadcrumb .= '» ' . $parent->title()->html();
    }
    echo $breadcrumb;
  ?>
  ...
<?php endforeach ?>
1 Like

Here’s my search template for anyone that’s looking for one. Copy this into search.php under templates.

<!-- site/templates/search.php -->
<?php snippet('header') ?>
<div class="text">

	<?php if($results): ?>
		<?php foreach($results as $result): ?>
			<div class="text">
					<h1><a href="<?php echo $result->url() ?>">

							<?php
							$breadcrumb = '';
							foreach($result->parents()->flip() as $parent) {
								echo  $parent->title()->html(). ' » ';

							}?>

							<?php echo $result->title() ?></a></h1>
						<p>
							<?php echo $result->description() ?> <?php echo excerpt($result->text(), 300) ?> [...] <a href="<?php echo $result->url() ?>">Read more.</a>
						</p>
			</div>
		<?php endforeach ?>
			<div>
				<?php snippet('pagination', array('pagination' => $results->pagination())) ?>
			</div>
	<?php elseif($search->query()): ?>
			<div>
				<h3>No posts found matching «<?php echo html($search->query()) ?>».</h3>
				<p>Your search for <strong><?php echo html($search->query()) ?></strong> returned no results.</p>
			</div>
	<?php endif ?>
			<h3>Tags</h3>
			<p>
				<?php foreach($tags as $tag): ?>
					<a href="<?php echo $tag->url() ?>">#<?php echo $tag->name() ?></a>
				<?php endforeach ?>
			</p>
</div>

<?php snippet('footer') ?>


2 Likes