How to get indents for code formatting right in Scriban

I'm trying to use a Scriban template for generating a C# model class, and it's working OK so far, except for indentation. The template looks like this:
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 }}
}
Then the rendered text looks like this:
using Cassandra.Mapping.Attributes;
namespace kyc_registration_msisdn_lookup;
public class kyc_registration_msisdn_lookup
{
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 kyc_registration_msisdn_lookup
{
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; }

}
What am I missing or doing wrong?
5 Replies
Angius
Angius2d ago
Try using the non-greedy whitespace removal? {{~ prop.access}} instead of {{- prop.access}} Also, Scriban has auto-indentation https://github.com/scriban/scriban/blob/master/doc/language.md#15-auto-indentation
VoidPointer
VoidPointerOP2d ago
Thank you both, @ZZZZZZZZZZZZZZZZZZZZZZZZZ and @Convoluted Style Sheets ! My class is now 99.9% like I wanted it. The only, very minor, problem left is an empty line between the opening { and the first property and an empty line after the last property before the closing }:
No description
VoidPointer
VoidPointerOP2d ago
I think I'm going to revert to T4 to see how it compares
Angius
Angius2d ago
I just use a stringbuilder and interpolation in my source generators lmao Case in point: https://github.com/Atulin/AutoDbSet/blob/master/AutoDbSet/AutoDbSetIncrementalSourceGenerator.cs
VoidPointer
VoidPointerOP2d ago
That looks like the neatest solution. I tried that once and gave up too early, because I was lazy and wanted a quick fix, but I think for future generator code I will use interpolation. Hehe, I kind of do, but for these generated model classes I could easily overlook that because we never actually work on the class's source code. For now, I'll gratefully accept the extra lines and close this post as it has helped me achieve what I wanted, the indentation is perfect now and I learned a lesson about it in Scriban.

Did you find this page helpful?