scriban

How do you parse booleans in Scriban?


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


Solution

  • 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.