Brand Logo
  • Home
  • Articles
  • Free Stuff
    • Copilot Tips Guide 🆕
    • Free JSON Formatting Mini-Guide
    • Code Samples
  • SharePoint Training
  • About
    • Coaching
  • Home
  • Articles
  • Free Stuff
  • SharePoint Training
  • About

A Beginner’s Guide to PnP Modern Search Templating

Posted on July 31, 2024
PnP Modern Search is a powerful tool for creating custom search experiences in SharePoint Online. One of the main features that makes PnP Modern Search so powerful is its templating engine. In this post, we’ll explore how PnP Modern Search templating works. I’ll also provide an overview of the different components involved.
 

What is PnP Modern Search Templating?

PnP Modern Search templating is a feature that allows you to customize the layout and design of your search results pages using Handlebars templates. Even if you pick the pre-built layouts (Details, People, Cards, etc) a Handlebars template is used behind the scenes. Some people are aware of that, and even use those templates as starting points for their own custom templates.
 

How Do PnP Modern Search Templates Work?

You may already be familiar with the web parts the make up PnP Modern Search, but if you’re not, I’ve got an online course to get you up to speed. PnP Modern Search templates use Handlebars templates to render custom templates. Here’s a high-level overview of how it works:
  1. Template Definition: You define a template using Handlebars syntax. The template defines the layout, fields, and formatting of your search results.
  2. Template Rendering: When a user performs a search, PnP Modern Search renders the template using the search results data.

Components of PnP Modern Search Templating

Here are the different components involved in PnP Modern Search templating:
  1. Handlebars Templates: Handlebars is a templating engine that allows you to define templates using a simple syntax. It’s built into some of the PnP Modern Search web parts automatically (such as the search results web part).
  2. Template Helpers: Template helpers are functions that can be used to manipulate the search results data and render it in the template. They serve as shortcuts to add functionality, saving you from having to be a developer.
  3. Template Partials: Template partials are reusable blocks of HTML that can be used to render specific parts of the search results. If that sounds confusing, just ignore them. It’s much more simple in practice, as you’ll see in future blog posts.

Creating a Custom Template

Creating a custom template in PnP Modern Search involves defining a Handlebars template and using template helpers and partials to render the search results data.
 

Example Template Code

Here’s an example of a simple template that displays the title, description, and URL of each search result:
{{#each data.items}}
  <div class="search-result">
    <h2></h2>
    <p></p>
    <a href=""></a>
  </div>
 
I’ll cover what these weird brackets do in future posts, but what’s important to understand is that this will loop over each search result and repeat the HTML DIV (and its contents) for each search result. This means you only have to define what the first search result looks like, and all the other results will look the same. Efficient, right?? The title, description, and the URL will be displayed for each result. You’ve got full control over what is displayed, and how it’s displayed. That’s the real power of custom templates. 
 
 

Conclusion

PnP Modern Search templating is a powerful feature that allows you to customize the layout and design of your search results pages. By using Handlebars templates, template helpers, and partials, you can create custom templates that meet your specific needs. In this blog post, I provided an overview of how PnP Modern Search templating works, and provided an example template code to get you familiar with how these things look.
 

What’s Next?

If you’re interested in learning more about PnP Modern Search templating, I encourage you to stay tuned to my blog as well as my YouTube channel. I’ll be covering this in more detail in the future. 
 
If you’re not ready for custom templates, but  you’re interested in learning more about PnP Modern Search, and how to configure it, I’ve got an online course that’ll walk you through the whole thing!

Contact Me!

    Previous Post
    The Latest SharePoint Premium Feature Is Actually The Best One. I Think?
    Next Post
    SharePoint Premium vs Microsoft Copilot for M365

    Recent Posts

    Copilot Tools: SharePoint Agents, Copilot Studio Lite, and Copilot Studio

    1 Oct at 5:40 pm

    Breaking Down The New SharePoint Knowledge Agent

    19 Sep at 7:32 am

    Copilot Agents Are Quietly Slashing IT Workloads. Here’s How

    10 Sep at 7:45 am

    AI for SMB: Real-World Wins Inside The Microsoft Ecosystem

    4 Sep at 6:50 am

    How To Delete SharePoint Agents

    26 Aug at 7:10 am

    Keep Up With Microsoft

    Stay Ahead of SharePoint & M365 Copilot without the overwhelm.

    Book Recommendation
    Mastering Microsoft 365 and SharePoint Online: A complete guide to boosting organizational efficiency with Microsoft 365’s real-world solutions
    TikTok
    X
    LinkedIn
    YouTube
    *As an Amazon Associate I earn a commission from qualifying purchases.