Too Much Indentation When Rendering a Scriban Template

YI have this little Scriban template for generating a simple C# class:
using Cassandra.Mapping.Attributes;

namespace {{model.name_space}};

public class {{model.Name}}
{
{{ for prop in model.properties }}
{{ if model.is_partition_key }}[PartitionKey]{{ end }}
{{prop.access}} {{prop.type_name}} {{prop.name}} { get; set; }
{{ end }}
}
using Cassandra.Mapping.Attributes;

namespace {{model.name_space}};

public class {{model.Name}}
{
{{ for prop in model.properties }}
{{ if model.is_partition_key }}[PartitionKey]{{ end }}
{{prop.access}} {{prop.type_name}} {{prop.name}} { get; set; }
{{ end }}
}
You can see the indentation for property declarations is 4 spaces, yet when I render the template, I get this:
using Cassandra.Mapping.Attributes;

namespace kyc_registration_msisdn_lookup;

public class
{


public int hanis_image_status { get; set; }


public string identifier { get; set; }


public string identity_number { get; set; }


public Guid kyc_id { get; set; }


public DateTime modified_datetime { get; set; }


public string organization_code { get; set; }

}
using Cassandra.Mapping.Attributes;

namespace kyc_registration_msisdn_lookup;

public class
{


public int hanis_image_status { get; set; }


public string identifier { get; set; }


public string identity_number { get; set; }


public Guid kyc_id { get; set; }


public DateTime modified_datetime { get; set; }


public string organization_code { get; set; }

}
where we have an indent of 8 spaces for the properties, and extra empty lines between the properties. What can I do to set the indentation or formatting for this template?
3 Replies
Angius
Angius3w ago
Try playing around with whitespace control
Thinker
Thinker3w ago
tl;dr for all the {{ }} on their own lines, you'll have to use {{~ ~}} to strip the whitespace properly
VoidPointer
VoidPointerOP3w ago
Thank you for all your very helpful suggestions. I couldn't get any of the whitespace control working, except for public class {{model.Name -}}, where the minus sign causes the extra blank line after the public class <className> , but for the indentation, I had to reduce that from 4 to 2 spaces in my template, so now the template renders 4 spaces, like I want, not the 8 it was rendering. It's somehow doubling the indent size. Scriban is by default set to autoindent, and the section of the docs about indentation says there is an option to set TemplateContext.AutoIndent to true, but I can't find how to apply the TemplateContext object to my tempate's parsing or rendering. I have cloned the Scriban source and will later hopefully figure this out with that as a reference, but for now I have things basically working and can get on with other stuff first, before bothering about the prettiness of my generated class.

Did you find this page helpful?