Kinetq Development Group, LLC. - Liquid Filters

Liquid Filters

Custom Liquid filters are created in a similar fashion to routes: implement ILiquidFilter and return a LiquidFilter object that pairs a filter name with a delegate. This design keeps filter logic encapsulated and testable while allowing full dependency injection.

  • Filters implement the ILiquidFilter interface and define a GetFilter() method that returns a LiquidFilter.

  • The LiquidFilter contains a Name (the identifier used in templates) and a FilterDelegate—the actual function that performs the transformation.

  • The FilterDelegate comes from the Fluid library and must return a value derived from FluidValue (e.g., ArrayValueObjectValueStringValue).

  • Because filters are registered in the DI container, they can consume any service, such as a search index or database context.

  • The example below shows a filter that retrieves featured articles and returns them as an array of view models:

public class GetFeaturedArticlesFilter : ILiquidFilter
{
    private readonly IIndexProvider _indexProvider;

    public GetFeaturedArticlesFilter(IIndexProvider indexProvider)
    {
        _indexProvider = indexProvider;
    }

    public async Task<LiquidFilter> GetFilter()
    {
        return new LiquidFilter()
        {
            Name = "get_featured_articles",
            FilterDelegate = GetFeaturedArticles
        };
    }

    private async ValueTask<FluidValue> GetFeaturedArticles(FluidValue input, FilterArguments arguments, TemplateContext context)
    {
        string filterInput = input.ToStringValue();

        var blogPosts =
            (await _indexProvider.Search()
                .Must(() => new TermQuery(new Term(nameof(BlogPost.IsPublished), Boolean.TrueString)))
                .Sort(() => new SortField(nameof(BlogPost.PublishedDate), SortFieldType.INT64, true))
                .Paged(1, 4)
                .ListResult<BlogPost>()).Hits
            .Select(x => x.Hit)
            .ToList();

        var featuredBlogPosts = new List<FluidValue>();
        foreach (var blogPost in blogPosts)
        {
            var blogPostViewModel = new LinkViewModel()
            {
                Title = blogPost.Name,
                Url = $"{blogPost.Path}"
            };

            featuredBlogPosts.Add(new ObjectValue(blogPostViewModel));
        }

        return new ArrayValue(featuredBlogPosts);
    }
}

Do you have more questions? Contact us

Contact us