I have to list my upcoming events on the web page. I have created the data source item for holding all the events and each event has event date. I'm using Scriban to display all the events. My question is how should I ignore the passed day events and have to do the order by event date.
This is my Scriban Code snippet:
<div>
{{ for i_child in i_datasource.children }}
<div>
<h1><a href="{{ sc_link_url i_child 'CTA'}}"> {{ sc_field i_child 'Title'}}</a></h1>
</div>
{{ end }}
</div>
Finally Done
As mentioned here How do you create a scriban function which implements the Content Search API? I have created the new search method for my requirements.
Created new class file
public class GetSortOrder : IGenerateScribanContextProcessor
{
protected ISearchService SearchService { get; }
protected ISortingService SortingService { get; }
protected ISiteInfoResolver SiteInfoResolver { get; }
public GetSortOrder(ISearchService searchService, ISortingService sortingService)
{
SearchService =searchService;
SortingService = sortingService;
}
private delegate IEnumerable<Item> SearchItemsDelegate(Item item, string s, string q = null, string o = null, int p = 10, int e = -1, string g = null, double r = -1);
/// <summary>
/// Search API
/// </summary>
/// <param name="s">ScopeID</param>
/// <param name="p">NumberOfResults</param>
/// <param name="o">Order</param>
/// <param name="f">OrderBy FieldName</param>
/// <returns>Result Items</returns>
public IEnumerable<Item> SearchItemsImpl(string s, int p = 10, int o = 0, string f = null)
{
try
{
var scopesIDs = s?.Split(',', '|').Where(ID.IsID).Select(ID.Parse);
var query = this.SearchService.GetQuery(new SearchQueryModel
{
ScopesIDs = scopesIDs,
}, out var indexName);
//if o=1 then ascending
if (o == 1)
query = query.OrderBy((ContentPage i) => i.get_Item<double>((string)f));
else
query = query.OrderByDescending((ContentPage i) => i.get_Item<double>((string)f));
if (p > 0)
{
query = query.Take(p);
}
return query.Select(i => i.Uri).ToList()
.Select(u => Factory.GetDatabase(u.DatabaseName)
.GetItem(u.ItemID, u.Language, u.Version));
}
catch (Exception ex)
{
Log.Info(string.Format("SearchItemsImpl-error-{0}stacktrace{1}", ex.Message.ToString(), ex.StackTrace.ToString()), ex);
}
return null;
}
public void Process(GenerateScribanContextPipelineArgs args)
{
args.GlobalScriptObject.Import("sc_searchitems", new SearchItemsDelegate(SearchItemsImpl));
}
}
Added in Patch file
<processor type="Foundation.ScribanExtensions.Pipelines.GetSortOrder, Foundation.ScribanExtensions" resolve="true" />
Updated scriban
{{ for i_searchitem in ( sc_searchitems i_datasource "{D7C469EF-8B47-4B19-810F-3ACA1354D979}" o: "EventDate,Descending" p: 50 q:"./*[@@templatename='Events'] " ) }}
<li>{{ i_searchitem.Fields['Title'] }}</li>
{{ end }}
Here, I have mentioned my field EventDate, not working. I could see, it is working as expected with Title Field. So, something i have to add for Date field, s