Why I cannot get size of the string returned from function in scriban?

376 Views Asked by At

I am testing following code in scriban:

{{ func getString }}text{{ end }}

However when I try to get text length with:

{{ getString | string.size }}

I always get 0, whereas I expect 4. Why is this?

1

There are 1 best solutions below

0
On BEST ANSWER

Function getString as defined in question prints "text" to output and returns nothing as result and thus the 0 output of string.size on its result. You may treat this instruction as a call to printf or Console.Writeline - it prints string to output but you cannot do anything more with that string. To return a string from a method to be able to further process it you need to use ret:

{{ func getString
    ret 'text'
end }}

Then output of

{{ getString | string.size }}

will be 4.