Others

How to Use Conditional Formatting Syntax for PnP Modern Search Templates in SharePoint

Rana GarciaRana Garcia Oct 26, 2026 868 views

Question details

The user needs to apply different CSS styles within a PnP Modern Search Web Part template based on conditional logic, specifically when a column is empty or contains fewer than two characters.

How to Use Conditional Formatting Syntax for PnP Modern Search Templates
Product
SharePoint PnP Modern Search
Device & OS
not provided
Scenario
Customizing the layout and visual presentation of search results using Handlebars syntax and conditional CSS styling in a web part.
Observed behavior
The user is looking for the exact syntax and expressions to evaluate column content length and emptiness within the template structure.
Before you start

Ensure you have Site Collection Administrator privileges in your SharePoint environment and possess a basic understanding of Handlebars templating syntax, which is required for editing PnP Modern Search web parts.

Solution 1Recommended

Use Handlebars Helpers for Conditional CSS Application

Use the built-in Handlebars syntax provided by PnP Modern Search to evaluate column values and dynamically inject CSS classes into your HTML elements.

PnP Modern Search Web Parts use Handlebars as their templating engine. You can leverage block helpers like 'if' and specific comparison operators to check if a field contains data or meets a specific length requirement.

1
Edit the Web Part properties

Navigate to your SharePoint page, click 'Edit', and open the property pane for the PnP Search Results Web Part.

2
Access the custom template layout

Under the 'Layouts' section, select 'Custom' to reveal the HTML and Handlebars code editor for your search results.

3
Apply conditional formatting syntax

Locate the element you wish to style. Use a Handlebars expression such as {{#if item.YourColumnName}} class='has-data' {{else}} class='empty-data' {{/if}} to inject dynamic CSS classes based on the column's state.

4
Save and publish the page

Save the template modifications and republish your SharePoint page to test the newly applied conditional CSS styles.

Use Handlebars Helpers for Conditional CSS Application
Helper Availability: The availability of specific helpers like string length evaluations (e.g., {{length}}) depends on the exact version of the PnP Modern Search web part you have installed.
Free Microsoft Office alternative

Looking for a Lightweight Office Alternative?

While SharePoint handles your enterprise portals and complex web part formatting, WPS Office provides a free, highly compatible, and lightweight alternative to Microsoft Office for your daily document and data processing needs.

  1. 1. Download the installer: Visit the official WPS Office website and click the free download button for your operating system.
  2. 2. Run the installation: Open the downloaded setup file and follow the simple on-screen instructions to install the suite.
  3. 3. Open your documents seamlessly: Launch WPS Office and immediately open, edit, and save your existing Microsoft Office files with perfect formatting retention.
Draft, organize, and document your complex PnP search HTML/CSS code snippets easily within WPS Writer.Fully compatible with Microsoft Word, Excel, and PowerPoint file formats (.docx, .xlsx, .pptx) for seamless file sharing.Lightweight architecture ensures swift performance and minimal resource consumption, even on older devices.Familiar and intuitive user interface requires zero learning curve when migrating from standard Microsoft Office tools.
microsoft office alternative - wps office

Frequently Asked Questions

What templating language does PnP Modern Search use?

PnP Modern Search Web Parts use Handlebars as their primary templating engine. This allows users to combine standard HTML with Handlebars expressions to dynamically render data and apply conditional CSS styles.

Can I apply custom CSS classes directly in PnP Search templates?

Yes, you can write inline CSS styles or inject custom CSS class names into your HTML elements based on field values using Handlebars conditional block helpers like {{#if}} or {{#eq}}.

Where can I find official help for SharePoint PnP Modern Search syntax?

Advanced templating and coding questions are best suited for the Microsoft Q&A SharePoint development community or the official GitHub repository for PnP Modern Search, where active contributors and developers assist with complex syntaxes.