{"id":13594743,"url":"https://github.com/Tyrrrz/MiniRazor","last_synced_at":"2025-04-09T10:31:55.201Z","repository":{"id":42973942,"uuid":"283505885","full_name":"Tyrrrz/MiniRazor","owner":"Tyrrrz","description":"Portable Razor compiler \u0026 code generator","archived":true,"fork":false,"pushed_at":"2023-07-16T23:01:10.000Z","size":168,"stargazers_count":220,"open_issues_count":0,"forks_count":24,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-05-22T04:33:47.978Z","etag":null,"topics":["compilation","csharp-sourcegenerator","hacktoberfest","razor","razor-templates","roslyn","template-engine","templates"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tyrrrz.png","metadata":{"files":{"readme":"Readme.md","changelog":"Changelog.md","contributing":null,"funding":null,"license":"License.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"Tyrrrz","patreon":"Tyrrrz","custom":["buymeacoffee.com/Tyrrrz","tyrrrz.me/donate"]}},"created_at":"2020-07-29T13:25:47.000Z","updated_at":"2024-08-01T16:39:18.856Z","dependencies_parsed_at":"2024-08-01T16:54:02.392Z","dependency_job_id":null,"html_url":"https://github.com/Tyrrrz/MiniRazor","commit_stats":{"total_commits":135,"total_committers":6,"mean_commits":22.5,"dds":0.4740740740740741,"last_synced_commit":"ac55b845d36375fb57ff72f9ccc907dd4898d1c4"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tyrrrz%2FMiniRazor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tyrrrz%2FMiniRazor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tyrrrz%2FMiniRazor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tyrrrz%2FMiniRazor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tyrrrz","download_url":"https://codeload.github.com/Tyrrrz/MiniRazor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247846728,"owners_count":21006088,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compilation","csharp-sourcegenerator","hacktoberfest","razor","razor-templates","roslyn","template-engine","templates"],"created_at":"2024-08-01T16:01:38.608Z","updated_at":"2025-04-09T10:31:53.387Z","avatar_url":"https://github.com/Tyrrrz.png","language":"C#","funding_links":["https://github.com/sponsors/Tyrrrz","https://patreon.com/Tyrrrz","buymeacoffee.com/Tyrrrz","tyrrrz.me/donate"],"categories":["hacktoberfest","C#","Source Generators","C\\#"],"sub_categories":["Webprogramming"],"readme":"# MiniRazor\n\n[![Status](https://img.shields.io/badge/status-discontinued-e4181c.svg)](https://github.com/Tyrrrz/.github/blob/master/docs/project-status.md)\n[![Made in Ukraine](https://img.shields.io/badge/made_in-ukraine-ffd700.svg?labelColor=0057b7)](https://vshymanskyy.github.io/StandWithUkraine)\n[![Build](https://img.shields.io/github/actions/workflow/status/Tyrrrz/MiniRazor/main.yml?branch=master)](https://github.com/Tyrrrz/MiniRazor/actions)\n[![Coverage](https://img.shields.io/codecov/c/github/Tyrrrz/MiniRazor/master)](https://codecov.io/gh/Tyrrrz/MiniRazor)\n[![Version](https://img.shields.io/nuget/v/MiniRazor.svg)](https://nuget.org/packages/MiniRazor)\n[![Downloads](https://img.shields.io/nuget/dt/MiniRazor.Runtime.svg)](https://nuget.org/packages/MiniRazor.Runtime)\n[![Discord](https://img.shields.io/discord/869237470565392384?label=discord)](https://discord.gg/2SUWKFnHSm)\n[![Fuck Russia](https://img.shields.io/badge/fuck-russia-e4181c.svg?labelColor=000000)](https://twitter.com/tyrrrz/status/1495972128977571848)\n\n\u003ctable\u003e\n    \u003ctr\u003e\n        \u003ctd width=\"99999\" align=\"center\"\u003eDevelopment of this project is entirely funded by the community. \u003cb\u003e\u003ca href=\"https://tyrrrz.me/donate\"\u003eConsider donating to support!\u003c/a\u003e\u003c/b\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\n\u003e **Note**:\n\u003e As an alternative, consider using [RazorBlade](https://github.com/ltrzesniewski/RazorBlade) for build-time template compilation and [RazorLight](https://github.com/toddams/RazorLight) for run-time template compilation.\n\n**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.\n\n## Terms of use\u003csup\u003e[[?]](https://github.com/Tyrrrz/.github/blob/master/docs/why-so-political.md)\u003c/sup\u003e\n\nBy 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:\n\n- You **condemn Russia and its military aggression against Ukraine**\n- You **recognize that Russia is an occupant that unlawfully invaded a sovereign state**\n- You **support Ukraine's territorial integrity, including its claims over temporarily occupied territories of Crimea and Donbas**\n- You **reject false narratives perpetuated by Russian state propaganda**\n\nTo learn more about the war and how you can help, [click here](https://tyrrrz.me). Glory to Ukraine! 🇺🇦\n\n## Install\n\nAll-in-one meta package:\n\n- 📦 [NuGet](https://nuget.org/packages/MiniRazor): `dotnet add package MiniRazor`\n\nSpecialized packages:\n\n- 📦 [NuGet](https://nuget.org/packages/MiniRazor.Compiler): `dotnet add package MiniRazor.Compiler` (**run-time compilation** only)\n- 📦 [NuGet](https://nuget.org/packages/MiniRazor.CodeGen): `dotnet add package MiniRazor.CodeGen` (**build-time compilation** only)\n\n\u003e ⚠ If you're referencing **MiniRazor.CodeGen**, ensure that it's **NOT** marked as a development dependency!\n\u003e 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.\n\n## Usage\n\n### Compiling templates at build-time\n\n\u003e **Warning**:\n\u003e Compiling at build time requires [MiniRazor](#install) or [MiniRazor.CodeGen](#install).\n\n**MiniRazor** comes with a source generator that can parse Razor templates and transpile them into C# classes directly at build time.\nThis workflow is suitable and highly recommended for scenarios where your templates are not expected to change.\n\nTo do that, first create a Razor template as shown here:\n\n```razor\n@inherits MiniRazor.TemplateBase\u003cstring\u003e\n@namespace MyNamespace.Templates\n\n\u003chtml\u003e\n\n\u003chead\u003e\n    \u003ctitle\u003eHello @Model\u003c/title\u003e\n\u003c/head\u003e\n\n\u003cbody\u003e\n    \u003cp\u003eHello @Model\u003c/p\u003e\n\u003c/body\u003e\n\n\u003c/html\u003e\n```\n\nNote the usage of two important directives at the top of the file:\n\n- `@inherits` directive indicates that the base type of this template is `MiniRazor.TemplateBase\u003cTModel\u003e`, with the model of type `string`.\n  If this directive is not included, the template will inherit from `MiniRazor.TemplateBase\u003cdynamic\u003e` instead — providing no type-safety when working with the model.\n- `@namespace` directive instructs the compiler to put the generated template class into the `MyNamespace.Templates` namespace.\n  If this directive is not included, the default namespace of `MiniRazor.GeneratedTemplates` will be used instead.\n\nIn 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:\n\n```xml\n\u003cProject Sdk=\"Microsoft.NET.Sdk\"\u003e\n\n  \u003cPropertyGroup\u003e\n    \u003cOutputType\u003eExe\u003c/OutputType\u003e\n    \u003cTargetFramework\u003enet6.0\u003c/TargetFramework\u003e\n  \u003c/PropertyGroup\u003e\n\n  \u003cItemGroup\u003e\n    \u003c!-- Include a single template --\u003e\n    \u003cAdditionalFiles Include=\"Templates/TemplateFoo.cshtml\" IsRazorTemplate=\"true\" /\u003e\n\n    \u003c!-- Include multiple templates at once --\u003e\n    \u003cAdditionalFiles Include=\"Templates/*.cshtml\" IsRazorTemplate=\"true\" /\u003e\n  \u003c/ItemGroup\u003e\n\n  \u003c!-- ... --\u003e\n\n\u003c/Project\u003e\n```\n\nAfter that, you should be able to run `dotnet build` to trigger the source generator and generate the corresponding template classes.\nThe template from the above example will then become available as the `MyNamespace.Templates.TemplateFoo` class.\nTo render it, you can call its `RenderAsync(...)` static method:\n\n```csharp\n// Reference the namespace where the template is located\nusing MyNamespace.Templates;\n\n// Render the template to a string, with @Model set to \"world\"\nvar output = await TemplateFoo.RenderAsync(\"world\");\n\n// Or, alternatively, render it to the specified TextWriter\nawait TemplateFoo.RenderAsync(Console.Out, \"world\");\n```\n\nNote that the type of the `model` parameter in `RenderAsync(...)` is automatically inferred based on the `@inherits` directive specified in the template.\nHere, since the template is derived from `MiniRazor.TemplateBase\u003cstring\u003e`, the method expects a parameter of type `string`.\n\n### Compiling templates at run-time\n\n\u003e **Warning**:\n\u003e Compiling at run-time requires [MiniRazor](#install) or [MiniRazor.Compiler](#install).\n\nIf the previous approach doesn't fit your usage scenario, you can also compile templates at run-time.\nTo do that, call `Razor.Compile(...)` with the template's source code:\n\n```csharp\nusing MiniRazor;\n\n// Compile the template into an in-memory assembly\nvar template = Razor.Compile(\"\u003cp\u003eHello, @Model.Subject!\u003c/p\u003e\");\n\n// Render the template to a string\nvar output = await template.RenderAsync(new MyModel { Subject = \"World\" });\n// \u003cp\u003eHello, World!\u003c/p\u003e\n```\n\nCalling `Razor.Compile(...)` transforms the provided Razor template directly into IL code hosted in a dynamic in-memory assembly.\nThis returns an instance of `TemplateDescriptor`, which you can then use to render output.\n\nBy default, **MiniRazor** uses the default assembly load context, which means that the compiled IL code will stay in memory forever.\nTo avoid that, you can pass a custom instance of `AssemblyLoadContext` that lets you control the lifetime of the generated assemblies:\n\n```csharp\n// Create an isolated assembly load context\nvar alc = new AssemblyLoadContext(\"MyALC\", true);\n\n// Compile the template\nvar template = Razor.Compile(\"\u003cp\u003eHello, @Model.Subject!\u003c/p\u003e\", alc);\n\n// Unload the ALC once it's no longer needed\nalc.Unload();\n```\n\n### Templating features\n\n#### HTML encoding\n\nOutput rendered with Razor templates is HTML-encoded by default.\nIf you want to print raw HTML content, for example if it's sourced from somewhere else, you can use the `Raw(...)` method:\n\n```razor\n@{\n    string GetHtml() =\u003e \"\u003cp\u003eHello world!\u003c/p\u003e\";\n}\n\n@GetHtml() // \u0026lt;p\u0026gt;Hello world!\u0026lt;/p\u0026gt;\n@Raw(GetHtml()) // \u003cp\u003eHello world!\u003c/p\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTyrrrz%2FMiniRazor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTyrrrz%2FMiniRazor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTyrrrz%2FMiniRazor/lists"}