How to flexibly manage data completeness in liquid templates

Passing params to the query

There is an interesting possibility of the paginate filter I want to share.

Assume that on the product page you want to get product property values of the current category products in the liquid template using collection.products property. E.g. we want to useproduct.properties here:

By default, product property values are not included into current category products, as they are commonly not needed.

One way is to fill properties for all category products every time. Thus we would query and pass the data that would be redundant in most cases.
Instead, we could ask for additional data inside the template using paginate filter params:

Here we specified ‘{ “ResponseGroup” : “Default, ItemProperties” }’ as a parameters for the paginate filter. And it applies specified ResponseGroup for the products! The magic is described below:

  1. WorkContext.Collection is of Category type, which has Products property of MutablePagedList type;
  1. Paginate filter, which is called in paginator, parses the parameters into NameValueCollection. It has the specific handling for the IMutablePagedList. It passes the parameters to the IMutablePagedList.Slice method.
  1. MutablePagedList has the constructor with getter parameter, which is used for getting data by the Slice method:
  1. category.Products is created via this constructor with the getter implementation:
  1. When the template is rendered, category products are queued. Search criteria is populated with the params we passed:

Note that we can fill any searchCriteria property using this technique, ReponseGroup is just an example.

Other features

There are some other nice features of MutablePagedList, besides pagination.
For example, an ability to access specific collection elements using property value - e.g. product.properties.brand.
Collection elements should implement IAccessibleByIndexKey interface for enabling:


And that’s how MutablePagedList searches for the items:

Conslusion

I recommend using MutablePagedList for the data collections you are using in liquid templates. It gives pagination, flexibility and usage convinience over ordinary collections.

2 Likes