Building custom liquid filters

Loading

Liquid is a flexible and powerful templating language that can be used in Power Pages (formerly PowerApps Portals) to dynamically render content. It is widely used for customizing the layout, logic, and behavior of content within portals. Custom Liquid filters are essential for manipulating data, transforming it into the desired format, and enhancing the way data is presented.

This guide will walk you through the process of building custom Liquid filters in Power Pages.


1. Understanding Liquid Filters

A Liquid filter is a method used to modify or format data output in your portal pages. Filters in Liquid are commonly used to modify variables (such as strings, numbers, dates, etc.), perform mathematical operations, or manipulate data structures. Some standard filters in Liquid include date, upcase, downcase, and replace.

Example of Using a Built-in Filter in Liquid:

liquidCopyEdit{{ '2025-01-01' | date: "%Y-%m-%d" }}

This example uses the date filter to format a date variable into a specific format.


2. Creating Custom Liquid Filters

Power Pages allows you to extend the functionality of Liquid by creating custom filters. These filters allow you to perform more complex operations on the data that cannot be achieved with the built-in filters.

Liquid custom filters in Power Pages can be created by writing JavaScript functions and incorporating them into the portal through Web Files or JavaScript libraries. Below is a detailed step-by-step guide to creating custom Liquid filters.


Step 1: Set Up a JavaScript Web File

Custom Liquid filters in Power Pages often rely on JavaScript to provide the necessary logic for manipulating data. You’ll need to create a JavaScript Web File to store your custom filter logic.

  1. Create a new Web File:
    • Go to Power Pages Management.
    • Navigate to Web Files and click New.
    • Name your Web File (e.g., CustomLiquidFilters.js).
    • Set the Type as JavaScript.
  2. Add JavaScript Logic: In the Web File, write the custom JavaScript logic that defines the new filter. Here’s an example of a simple filter that capitalizes the first letter of each word in a string:
(function() {
// Create a custom Liquid filter that capitalizes the first letter of each word
Liquid.registerFilter('capitalizeWords', function(input) {
if (typeof input === 'string') {
return input.replace(/\b\w/g, function(letter) {
return letter.toUpperCase();
});
}
return input;
});
})();

In this example, the capitalizeWords filter capitalizes the first letter of each word in the given string.


Step 2: Link the JavaScript File to Power Pages

Once you have created the custom JavaScript file, you’ll need to link it to the Power Pages site to make it accessible for use.

  1. Reference the Web File in a Web Template:
    • Go to Web Templates in Power Pages and create a new Web Template (or edit an existing one).
    • Add the following code to include your custom JavaScript Web File:
<script src="/WebFiles/CustomLiquidFilters.js"></script>
  1. Publish the Web Template:
    • Once you’ve added the script reference to your Web Template, publish the Web Template to make it available on your portal pages.

Step 3: Use the Custom Liquid Filter in a Page

Now that the custom filter is available on your portal, you can use it in your Liquid templates (e.g., in your Web Pages, Web Templates, or Entity Forms). Here’s how to use the capitalizeWords filter in a Liquid template.

{% assign sentence = "hello world from power pages" %}
{{ sentence | capitalizeWords }}

This code will render the text as:

Hello World From Power Pages

Step 4: Debugging and Testing

After adding your custom filter, you should thoroughly test it to ensure it works as expected. Here are a few tips:

  1. Check for Syntax Errors: Verify that the JavaScript code has no errors by using the browser’s developer tools.
  2. Use Browser Console: Check the browser console for any JavaScript errors related to your custom filter.
  3. Test in Different Contexts: Make sure the custom filter works in various contexts (e.g., on different pages or forms).
  4. Handle Edge Cases: Ensure the custom filter handles edge cases, such as null values, empty strings, and unexpected data types.

Example of Complex Custom Filter:

Here’s an example of a more complex custom Liquid filter that formats a date into a human-readable format:

(function() {
Liquid.registerFilter('formatDate', function(input, format) {
if (input && format) {
var date = new Date(input);
var formattedDate = '';

switch (format) {
case 'short':
formattedDate = date.toLocaleDateString();
break;
case 'long':
formattedDate = date.toLocaleString();
break;
default:
formattedDate = date.toDateString();
break;
}

return formattedDate;
}
return input;
});
})();

In this example, the formatDate filter takes a format argument to decide how to display the date:

{% assign myDate = "2025-01-01" %}
{{ myDate | formatDate: "long" }}

The output would be a full date and time, such as:

Wednesday, January 1, 2025 12:00:00 AM

Step 5: Optimizing Custom Filters

  1. Performance Considerations: Keep in mind that complex JavaScript filters can impact the performance of your portal, especially when used on pages with heavy traffic. Try to minimize computational complexity and consider caching results if necessary.
  2. Error Handling: Ensure your filters handle unexpected input gracefully. For example, return a default value if the input is invalid.
  3. Reusable Filters: Build custom filters that are reusable and flexible. This helps keep your portal code clean and maintainable.

Best Practices for Creating Custom Liquid Filters:

  1. Keep It Simple: Try to create filters that solve common problems but remain simple and easy to use.
  2. Document Your Filters: If you have multiple custom filters, create documentation to explain how they work and what parameters they accept.
  3. Use Descriptive Names: Choose meaningful names for your filters that clearly describe their behavior.
  4. Test Extensively: Always test your custom filters thoroughly to ensure they work in all cases.

Leave a Reply

Your email address will not be published. Required fields are marked *