I have a pipe-separated string
www.test.com | www.test1.com
I want to get just the first string before the pipe that is www.test.com
.
How can I do this in the scriban template?
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communityScriban has a split function on strings: https://github.com/scriban/scriban/blob/master/doc/builtins.md#stringsplit
string.split <text> <match>
The split function takes on a substring as a parameter. The substring is used as a delimiter to divide a string into an array. You can output different parts of an array using array functions.
Also check the whitespace control if needed (I do see spaces around your pipes): https://github.com/scriban/scriban/blob/master/doc/language.md#14-whitespace-control
- The greedy mode using the character - (e.g {{- or -}}), removes any whitespace, including newlines
- The non greedy mode using the character ~
- Using a {{~ will remove any whitespace before but will stop on the first newline without including it
- Using a ~}} will remove any whitespace after including the first newline but will stop after
Working with arrays is explained at https://github.com/scriban/scriban/blob/master/doc/builtins.md#array-functions.
You could use array.first <list>
to get the first element.
Update
Try:
{{ i_item.URLList | string.split "|" | array.first }}
That should do it..
Maybe you can use the string split (by '|') function that will get you the array: [www.test.com, www.test1.com]. Then you can read first element of the array using array first function. Example:
{{ for word in "Hi, how are you today?" | string.split ' ' ~}}
{{ word }}
{{ end ~}}
output
Hi,
how
are
you
today?