230 sao GitHub và vẫn tăng — Tyrrrz/MiniRazor là dự án C# mà TopGit đang theo dõi trên nền tảng. Portable Razor compiler & code generator
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Development of this project is entirely funded by the community. Consider donating to support!
Note:
As an alternative, consider using RazorBlade for build-time template compilation and RazorLight for run-time template compilation.
MiniRazor is a tiny abstraction over the Razor engine, designed to provide a simple interface to compile and render templates, both during build and at run-time.
Terms of use[?]
By using this project or its source code, for any purpose and in any shape or form, you grant your implicit agreement to all the following statements:
You condemn Russia and its military aggression against Ukraine
You recognize that Russia is an occupant that unlawfully invaded a sovereign state
You support Ukraine's territorial integrity, including its claims over temporarily occupied territories of Crimea and Donbas
You reject false narratives perpetuated by Russian state propaganda
To learn more about the war and how you can help, click here. Glory to Ukraine! 🇺🇦
⚠ If you're referencing MiniRazor.CodeGen, ensure that it's NOT marked as a development dependency!
Although the source generator assembly itself is only used during build, this package also contains binaries which are required by the generated code at run-time.
Usage
Compiling templates at build-time
Warning:
Compiling at build time requires MiniRazor or MiniRazor.CodeGen.
MiniRazor comes with a source generator that can parse Razor templates and transpile them into C# classes directly at build time.
This workflow is suitable and highly recommended for scenarios where your templates are not expected to change.
To do that, first create a Razor template as shown here:
Note the usage of two important directives at the top of the file:
@inherits directive indicates that the base type of this template is MiniRazor.TemplateBase<TModel>, with the model of type string.
If this directive is not included, the template will inherit from MiniRazor.TemplateBase<dynamic> instead — providing no type-safety when working with the model.
@namespace directive instructs the compiler to put the generated template class into the MyNamespace.Templates namespace.
If this directive is not included, the default namespace of MiniRazor.GeneratedTemplates will be used instead.
In order to make the template accessible by MiniRazor's source generator, you need to add it to the project using the AdditionalFiles element and mark it with the IsRazorTemplate="true" attribute:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<!-- Include a single template -->
<AdditionalFiles Include="Templates/TemplateFoo.cshtml" IsRazorTemplate="true" />
<!-- Include multiple templates at once -->
<AdditionalFiles Include="Templates/*.cshtml" IsRazorTemplate="true" />
</ItemGroup>
<!-- ... -->
</Project>
After that, you should be able to run dotnet build to trigger the source generator and generate the corresponding template classes.
The template from the above example will then become available as the MyNamespace.Templates.TemplateFoo class.
To render it, you can call its RenderAsync(...) static method:
// Reference the namespace where the template is located
using MyNamespace.Templates;
// Render the template to a string, with @Model set to "world"
var output = await TemplateFoo.RenderAsync("world");
// Or, alternatively, render it to the specified TextWriter
await TemplateFoo.RenderAsync(Console.Out, "world");
Note that the type of the model parameter in RenderAsync(...) is automatically inferred based on the @inherits directive specified in the template.
Here, since the template is derived from MiniRazor.TemplateBase<string>, the method expects a parameter of type string.
Compiling templates at run-time
Warning:
Compiling at run-time requires MiniRazor or MiniRazor.Compiler.
If the previous approach doesn't fit your usage scenario, you can also compile templates at run-time.
To do that, call Razor.Compile(...) with the template's source code:
using MiniRazor;
// Compile the template into an in-memory assembly
var template = Razor.Compile("<p>Hello, @Model.Subject!</p>");
// Render the template to a string
var output = await template.RenderAsync(new MyModel { Subject = "World" });
// <p>Hello, World!</p>
Calling Razor.Compile(...) transforms the provided Razor template directly into IL code hosted in a dynamic in-memory assembly.
This returns an instance of TemplateDescriptor, which you can then use to render output.
By default, MiniRazor uses the default assembly load context, which means that the compiled IL code will stay in memory forever.
To avoid that, you can pass a custom instance of AssemblyLoadContext that lets you control the lifetime of the generated assemblies:
// Create an isolated assembly load context
var alc = new AssemblyLoadContext("MyALC", true);
// Compile the template
var template = Razor.Compile("<p>Hello, @Model.Subject!</p>", alc);
// Unload the ALC once it's no longer needed
alc.Unload();
Templating features
HTML encoding
Output rendered with Razor templates is HTML-encoded by default.
If you want to print raw HTML content, for example if it's sourced from somewhere else, you can use the Raw(...) method:
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/Tyrrrz/MiniRazor là nguồn chính thức.
Tyrrrz/MiniRazor có bao nhiêu sao?
Tyrrrz/MiniRazor có 230 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/Tyrrrz/MiniRazor. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
Tyrrrz/MiniRazor có những chủ đề gì?
GitHub topics của Tyrrrz/MiniRazor: "compilation", "csharp-sourcegenerator", "hacktoberfest", "razor", "razor-templates", "roslyn", "template-engine", "templates". TopGit xếp repo vào nhóm mã nguồn mở.
Tyrrrz/MiniRazor còn đang phát triển không?
Commit gần nhất trên Tyrrrz/MiniRazor là 3.3 năm trước (theo timestamp GitHub). Repo có 26 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
Tyrrrz/MiniRazor viết bằng ngôn ngữ gì?
Tyrrrz/MiniRazor chủ yếu viết bằng C#. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
Chưa chắc MiniRazor có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về MiniRazor.