Search code examples
c#dotliquid

Access collection properties within Dot Liquid


I'm using DotLiquid template engine to allow for theming in an application.

Within, I have a paginated list inheriting from List that is registered as a safe type, allowing access to the members within. The PaginatedList is coming from a higher tier in the application and is ignorant to the fact Dot Liquid is being used, hence the use of RegisterSafeType instead of inheriting Drop.

        Template.RegisterSafeType(typeof(PaginatedList<>), new string[] {
            "CurrentPage",
            "HasNextPage",
            "HasPreviousPage",
            "PageSize",
            "TotalCount",
            "TotalPages"
        });

 public class PaginatedList<T> : List<T>
{
    /// <summary>
    /// Returns a value representing the current page being viewed
    /// </summary>
    public int CurrentPage { get; private set; }

    /// <summary>
    /// Returns a value representing the number of items being viewed per page
    /// </summary>
    public int PageSize { get; private set; }

    /// <summary>
    /// Returns a value representing the total number of items that can be viewed across the paging
    /// </summary>
    public int TotalCount { get; private set; }

    /// <summary>
    /// Returns a value representing the total number of viewable pages
    /// </summary>
    public int TotalPages { get; private set; }

    /// <summary>
    /// Creates a new list object that allows datasets to be seperated into pages
    /// </summary>
    public PaginatedList(IQueryable<T> source, int currentPage = 1, int pageSize = 15)
    {
        CurrentPage = currentPage;
        PageSize = pageSize;
        TotalCount = source.Count();
        TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

        AddRange(source.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList());
    }

    /// <summary>
    /// Returns a value representing if the current collection has a previous page
    /// </summary>
    public bool HasPreviousPage
    {
        get
        {
            return (CurrentPage > 1);
        }
    }

    /// <summary>
    /// Returns a value representing if the current collection has a next page
    /// </summary>
    public bool HasNextPage
    {
        get
        {
            return (CurrentPage < TotalPages);
        }
    }
}

This list is then exposed to the view in local.Products, iterating the collection in Dot Liquid works fine.

However, I am trying to get access to the properties within, I'm not getting any errors but no values are being replaced by Dot Liquid.

I am using

{{ local.Products.CurrentPage }} |

Which is replaced with

  |

Can anyone see where I am going wrong?


Solution

  • I suspect it's not a problem with your code, but rather it's a limitation of how DotLiquid (and Liquid) handles lists and collections. IIRC, you can't access arbitrary properties on lists and collections.

    You can test that by changing your PaginatedList<T> so that it contains a List<T>, rather than inheriting from it.