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:
Then the rendered text looks like this:
What am I missing or doing wrong?
5 Replies
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-indentationThank 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 }
:
I think I'm going to revert to T4 to see how it compares
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
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.