How to Use Conditional Formatting Syntax for PnP Modern Search Templates in SharePoint
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.

- 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.
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.
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.
Navigate to your SharePoint page, click 'Edit', and open the property pane for the PnP Search Results Web Part.
Under the 'Layouts' section, select 'Custom' to reveal the HTML and Handlebars code editor for your search results.
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.
Save the template modifications and republish your SharePoint page to test the newly applied conditional CSS styles.

Consult the Microsoft Q&A SharePoint Development Community
Since custom web part templating falls under advanced SharePoint development, requesting syntax help from the official Microsoft developer forum is highly recommended.
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. Download the installer: Visit the official WPS Office website and click the free download button for your operating system.
- 2. Run the installation: Open the downloaded setup file and follow the simple on-screen instructions to install the suite.
- 3. Open your documents seamlessly: Launch WPS Office and immediately open, edit, and save your existing Microsoft Office files with perfect formatting retention.

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.




