How do you parse booleans in Scriban?

914 views Asked by At

Is it possible to perform bool.parse or similar operations?

Simplified Scriban template to demonstrate question:

var template = Template.Parse("{{ $parsed = foo | bool.parse }}");
var result = template.Render(new { foo = "True"});

This throws the error: (1,25) : error : Object bool is null

1

There are 1 answers

0
Bryan Euton On BEST ANSWER

Unfortunately there isn't a way parse booleans with a built in function. One work around is to do the following:

var template = Template.Parse("{{ if foo | string.downcase == `true`; $parsed = true; end; $parsed; }}");
var result = template.Render(new { foo = "True"});

Here is a link to the issue in github: https://github.com/lunet-io/scriban/issues/243.