2016-04-09 56 views
0

我使用DotLiquid模板引擎來允許在應用程序中進行主題化。Dot Liquid中的訪問集合屬性

其中,我有一個分段列表繼承List,註冊爲安全類型,允許訪問其中的成員。 PaginatedList來自應用程序中的更高層,並且對於Dot Liquid正在使用的事實無知,因此使用RegisterSafeType而不是繼承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); 
     } 
    } 
} 

該列表然後暴露在local.Products中的視圖中,迭代Dot Liquid中的集合可以正常工作。

但是,我試圖訪問內的屬性,我沒有得到任何錯誤,但沒有值被Dot Liquid所取代。

我使用

{{ local.Products.CurrentPage }} | 

這是替換

| 

任何人都可以看到,我錯了?

回答

1

我懷疑這不是代碼問題,而是DotLiquid(和Liquid)處理列表和集合的限制。 IIRC,你不能訪問列表和集合上的任意屬性。

您可以通過更改您的PaginatedList<T>以便它包含List<T>而不是從中繼承來測試。

+0

謝謝蒂姆,看起來就是這樣。 – EverythingGeek