
Show HN: Fast, Zero Allocation, AOT Safe, Compile-Time Safe .NET Template Engine
I built a text templating engine, that I've named Lissic, as part of a personal website project I'm working on. It fits a niche that I don't think anything else in the .NET ecosystem fills. I prefer to keep code out the…
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
I built a text templating engine, that I've named Lissic, as part of a personal website project I'm working on. It fits a niche that I don't think anything else in the .NET ecosystem fills. I prefer to keep code out the template itself (separation of concerns, view vs model, and all that). I also wanted to build something simple enough that I could imagine low/medium-tech UI dev working with it, but it still needs to build things in a modular way, support loops, support conditional/branching views, and have the ability to format/filter values inserted into the text.I've improved Lissic to the point that I think it's worth sharing with the community. A quick overview of the features:- Renders near twice as fast as the fastest major .NET templating engine (see below)
- Zero allocation with default settings, when rendering to a buffer (e.g. to HttpResponse.BodyWriter)
- 100% AOT compatible without any restrictions (no use of Reflection or dynamic)
- Simple but powerful tagging system that keeps a clear division between layout and logic
- Compile-time safety with source-generated Data Transfer Objects that automatically adjust to each template's text
- Define templates directly in code with text or import them from template files
- Transformation pipeline that allows using Formatters to modify how values are converted to text and Filters to modify text before insertion
- Hot reload for file-based templates in debug mode
- Thorough AI-friendly documentation (I gave only the documentation to QWEN3.8-27B, and it was able to make a simple task-management web app)A full BenchmarkDotNet chart is in the GitHub README, but here's a quick rendering comparison:Lissic (RenderToString) - 8.814 us / 18072 BLissic (RenderToBuffer) - 8.482 us / 0 BFluid - 15.593 us / 37128 BFluid (SourceGenerated) - 24.045 us / 49128 BScriban - 70.985 us / 144025 BHandlebars - 21.944 us / 122176 BIt uses tags that look similar to many other templating engines. It has tag types to insert direct values ("variables"), other templates ("parts"), and lists of templates ("loops"). It also has branching logic that works a lot like a switch statement (i.e. define keys, and the text and/or code that goes with each key).Very simple example:---------------------------------------------
[TemplateText("Hello {{ name }}!")]
public partial class LissicExample {}
// Automatically transforms the partial class into a DTO, with a property for each tag
LissicExample myTemplate = new() { name = "John" };
// LissicProcessor retrieved from Dependency Injection
lissicProcessor.RenderToBuffer(myTemplate, HttpResponse.BodyWriter);
// Writes "Hello JOHN!" to the HTTP response body
---------------------------------------------The main tradeoff at this time is that all templates are defined at startup (other than the debugging-only hot reload option), so it's not really a general purpose user-provided template engine. This leaves it comparable to libraries like Fluid or Scriban in some ways, but comparable to Razor Pages in others. It works really well for my purpose and preferences though.I'd appreciate any feedback anyone has. I'd like to keeping building this for the long haul. If nothing else, I'll be updating it for work in my own project.In particular, I'd like to support using literal text in loop tags, but that doesn't work cleanly with my current auto-generated DTO setup. I've considered auto-generating DTOs with predictable names (and providing a name override option via attributes). I've also considered supporting duck-typing for those, which would essentially be a new feature set, which is maybe useful or maybe over complicating it? I'd appreciate thoughts on this in particular. :)Shout out to Andrew Lock and his Creating a Source Generator series!The code is at https://github.com/gendeg/Lissic, and it's on NuGet (package name: Lissic) Comments URL: https://news.ycombinator.com/item?id=50281533 Points: 1
Comments: 1
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。