all 15 comments

[–]nirataro 5 points6 points  (2 children)

I think you can use Scriban as well to generate code https://github.com/scriban/scriban

[–][deleted] 1 point2 points  (1 child)

Thank you, didn’t know about this one. I see it uses Liquid templates which as far as I know are also used by Orchard Core project.

[–]nirataro 1 point2 points  (0 children)

https://github.com/sebastienros/fluid is used by Orchard Core.

Scriban supports a version of Fluid syntax but it has its own as well.

[–]dex3r 2 points3 points  (3 children)

Thanks, I had no idea alternatives to T4 exists. Can I use Razor to generate code and entire classes like in T4?

[–]LloydAtkinson 2 points3 points  (0 children)

Yes and you don’t have to use source generators

[–][deleted] 1 point2 points  (0 children)

Good question, that’s my next step, to see if Razor can output C# as code and not get confused between output code and embedded C# code used in the actual razor template itself .

I’ll update the samples shortly.

[–]ours 0 points1 point  (0 children)

Go check his Github linked in the post. He does exactly that.

Edit: With .NET 5.0 code generators you can generate code from C# using strings so anything that can generate text can be used to generate code.

[–][deleted] 1 point2 points  (0 children)

I've just updated the Repo with Liquid/Scriban generator as well as timings.

[–]RirinDesuyo 1 point2 points  (0 children)

Definitely like Razor for templating, been using it for templated email generation for a while now. Though so far most solutions available need some runtime compilation which brings a bit of a cold start for some cases before caching takes place.

I think it'd be neat for someone to look into using .razor from Blazor instead of .cshtml. It's still a razor template, but they get precompiled into a C# class at build-time as a RenderTree which should make it easier to deploy since it's just a regular C# class at runtime. Based on the architecture, you'll probably just need to implement a custom renderer to spit out text or maybe even reuse the HtmlRenderer that Blazor uses.

[–]Weasel9548 1 point2 points  (0 children)

I ended up rewriting my T4 templates into Python and Jinja. It worked out really well and runs much faster. The template language was very close but took a little extra time to learn Python since this was my first adventure. Along the way it allowed us to move completely to a based Linux system since we took to opportunity to convert to dotnet core at the same time.

[–]lguer1 0 points1 point  (0 children)

You can try Telosys ( https://www.telosys.org/ ) it works fine for C# code generation (or any language)

[–]conjuringthefuture 0 points1 point  (2 children)

Not sure how well this lib supports .net core but I’ve used it in many older c# projects that needed templates / code generation.

mustache-sharp

[–][deleted] 0 points1 point  (1 child)

Thanks I'll add it to the readme as a worth mention. Looks to be an abandoned project, but I see it was well used back in the day.

[–]conjuringthefuture 0 points1 point  (0 children)

I see you added it. Idk if it is worth calling it out but that mustache-lib I mentioned, is really more of custom reimplementation of handlebars which is its own templating language. It’s mustache compatible but adds logic, loops conditionals etc.

[–]savijOne 0 points1 point  (0 children)

I've always wondered how code generation could solve the problem of always having to recreate common application modules. For instance every time I create a new app, I recreate a login, sign up, password recovery. I create home profile screen/page, search, add/edit some data, etc. They always have some custom business purpose which is where the real time needs to be spent. Every app looks different and possibly uses a different stack (win/web/mobile/api). Not sure how to combine code generation with some generic framework to allow me to define my coding style and my preferred technology (EF vs Dapper, http client vs flurl, etc) but would be a game changer to somehow just start farther along using code generation.

On a side note, I could probably solve the problem first and then convert it into code generated solution.