Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save derekjones/c635017d20f09a16d7df219316a4b36c to your computer and use it in GitHub Desktop.
Save derekjones/c635017d20f09a16d7df219316a4b36c to your computer and use it in GitHub Desktop.
ExpressionEngine Search
{if layout:search_attorneys_count > 0 OR layout:search_offices_count > 0 OR layout:search_practice_areas_count > 0 OR layout:search_sub_practice_areas_count > 0 OR layout:search_representative_matters_count > 0 OR layout:search_insights_blogs_count > 0 OR layout:search_news_count > 0 OR layout:search_insights_count > 0 OR layout:search_awards_count > 0}
<div class="search-bar">
<ul class="search-bar-nav">
<li class="search-bar-nav-item">
<a href="#attorneys" class="search-bar-nav-link">All (<span id='search_count'>0</span>)</a>
</li>
{if layout:search_attorneys_count > 0}
<li class="search-bar-nav-item">
<a href="#attorneys" class="search-bar-nav-link">Attorneys ({layout:search_attorneys_count})</a>
</li>
{/if}
{if layout:search_offices_count > 0}
<li class="search-bar-nav-item">
<a href="#offices" class="search-bar-nav-link">Offices ({layout:search_offices_count})</a>
</li>
{/if}
{if layout:search_practice_areas_count > 0}
<li class="search-bar-nav-item">
<a href="#practice-areas" class="search-bar-nav-link">Practice Areas ({layout:search_practice_areas_count})</a>
</li>
{/if}
{if layout:search_sub_practice_areas_count > 0}
<li class="search-bar-nav-item">
<a href="#sub-practice-areas" class="search-bar-nav-link">Sub Practice Areas ({layout:search_sub_practice_areas_count})</a>
</li>
{/if}
{if layout:search_representative_matters_count > 0}
<li class="search-bar-nav-item">
<a href="#representative-matters" class="search-bar-nav-link">Representative Matters ({layout:search_representative_matters_count})</a>
</li>
{/if}
{if layout:search_news_count > 0}
<li class="search-bar-nav-item">
<a href="#news" class="search-bar-nav-link">News &amp; Resources ({layout:search_news_count})</a>
</li>
{/if}
{if layout:search_insights_count > 0}
<li class="search-bar-nav-item">
<a href="#insights" class="search-bar-nav-link">Insights ({layout:search_insights_count})</a>
</li>
{/if}
{if layout:search_awards_count > 0}
<li class="search-bar-nav-item">
<a href="#awards" class="search-bar-nav-link">Awards ({layout:search_awards_count})</a>
</li>
{/if}
<script>
var search_count = [
{layout:search_attorneys_count},
{layout:search_offices_count},
{layout:search_practice_areas_count},
{layout:search_sub_practice_areas_count},
{layout:search_representative_matters_count},
{layout:search_insights_blogs_count},
{layout:search_news_count},
{layout:search_insights_count},
{layout:search_awards_count},
].reduce(add, 0);
function add(a, b) {
return a + b;
}
document.getElementById('search_count').innerHTML = search_count;
</script>
</ul>
</div>
{layout:contents}
{if:else}
<p><em>No search results found for &ldquo;<strong>{route:keywords}</strong>&rdquo;</em>.</p>
{/if}
{layout='layouts/_search-results'}
{!-- news --}
{exp:low_search:results collection="news" orderby="entry_date" sort="desc" limit="9999" {global:disable} {global:cache}
keywords="{route:keywords}"
keywords:lang="en"
keywords:inflect="yes"
keywords:loose="no"
keywords:mode="{route:search_mode}"
}
{if no_results}
{layout:set name='search_news_count'}0{/layout:set}
{/if}
{layout:set name='search_news_count'}{total_results}{/layout:set}
{if count == 1}
<h2 id="news" class="search-listing-heading">news</h2>
<div class="search-listing listing listing-filled">
{/if}
<article class="listing-item">
<figure class="listing-item-image">
<a href="/about/our-news/{url_title}">
<img src="{new_image:url}" width="{new_image:width}" height="{new_image:height}" alt="{new_image:alt_text}">
</a>
</figure>
<div class="listing-item-content">
<div class="listing-item-context">{new_publication}</div>
<h2 class="listing-item-heading">
<a href="/about/our-news/{url_title}">{title}</a>
</h2>
<div class="listing-item-excerpt">
{new_excerpt}
</div>
<div class="listing-item-more">
<a href="/about/our-news/{url_title}" class="more">Read More</a>
</div>
</div>
</article>
{if count == total_results}
</div>
{/if}
{/exp:low_search:results}
{!-- awards --}
{exp:low_search:results collection="awards" orderby="entry_date" sort="desc" limit="9999" {global:disable} {global:cache}
keywords="{route:keywords}"
keywords:lang="en"
keywords:inflect="yes"
keywords:loose="no"
keywords:mode="{route:search_mode}"
}
{if no_results}
{layout:set name='search_awards_count'}0{/layout:set}
{/if}
{layout:set name='search_awards_count'}{total_results}{/layout:set}
{if count == 1}
<h2 id="awards" class="search-listing-heading">Awards</h2>
<div class="search-listing listing listing-filled">
{/if}
<article class="listing-item">
<figure class="listing-item-image">
<a href="/about/our-awards/{url_title}">
<img src="{award_image:url}" width="{award_image:width}" height="{award_image:height}" alt="{award_image:alt_text}">
</a>
</figure>
<div class="listing-item-content">
<div class="listing-item-context">{award_publication}</div>
<h2 class="listing-item-heading">
<a href="/about/our-awards/{url_title}">{title}</a>
</h2>
<div class="listing-item-excerpt">
{award_excerpt}
</div>
<div class="listing-item-more">
<a href="/about/our-awards/{url_title}" class="more">Read More</a>
</div>
</div>
</article>
{if count == total_results}
</div>
{/if}
{/exp:low_search:results}
{!-- repeat for each collection being searched, Template Partials could be used to stay DRY if you're using these markup blocks elsewhere --}
@derekjones
Copy link
Author

Should mention that there are other ways to go about this. For instance instead of calling multiple Low Search tags, I feel like you could combine that to a single tag, and use Layout Lists to still be able to break them out by collection on display, if the Low Search tag doesn't allow you to group the output by collection already.

@jeremyworboys
Copy link

Thanks Derek. I'll stick with the Stash implementation for this site since it's working currently, but I'll definitely have a go at this approach next time I run into a scenario like this.

P.S. It would be awesome if you could introduce some sort of simple arithmetic functionality to EE output tags like we now have for conditionals. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment