templatesrendersitecore-sxascriban

How to use array.filter in Scriban?


I have a created a template in a .scriban file, but it doesn't seem to work.

This is the snippet of the array I want to filter:

{{~ somevar = SomeArray | array.filter sub(sv) | array.first ~}}

And the custom function that should be called is:

{{func sub(sv)
   ret sv.Code == "99"
end}}

It seems that it calls the custom function, but I receive this error:

Scriban.Syntax.ScriptRuntimeException: '<input>(2,12) : error : Cannot get the member sv.Code for a null object.'

What am I doing wrong here?


Solution

  • I know this is really late, but I was looking for a example of how to use array.filter as well.

    You are calling sub(sv) and returning it's return value to array.filter. Instead, you need to pass the "address" of sub to array.filter.

    {{~ somevar = SomeArray | array.filter @sub | array.first ~}}
    

    This should work.