MiniRazor.Compiler 2.2.2

.NET Core 3.0 .NET Standard 2.0
NuGet\Install-Package MiniRazor.Compiler -Version 2.2.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package MiniRazor.Compiler --version 2.2.2
<PackageReference Include="MiniRazor.Compiler" Version="2.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MiniRazor.Compiler --version 2.2.2
#r "nuget: MiniRazor.Compiler, 2.2.2"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install MiniRazor.Compiler as a Cake Addin
#addin nuget:?package=MiniRazor.Compiler&version=2.2.2

// Install MiniRazor.Compiler as a Cake Tool
#tool nuget:?package=MiniRazor.Compiler&version=2.2.2

MiniRazor

Made in Ukraine Build Coverage Version Downloads Discord Donate Fuck Russia

🟑 Project status: maintenance mode<sup>[?]</sup>

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<sup>[?]</sup>

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! πŸ‡ΊπŸ‡¦

Install

All-in-one meta package:

  • πŸ“¦ NuGet: dotnet add package MiniRazor

Specialized packages:

  • πŸ“¦ NuGet: dotnet add package MiniRazor.Compiler (run-time compilation only)
  • πŸ“¦ NuGet: dotnet add package MiniRazor.CodeGen (build-time compilation only)

⚠ If you're referencing MiniRazor.CodeGen, ensure that it's NOT marked with PrivateAssets="all"! 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:

@inherits MiniRazor.TemplateBase<string>
@namespace MyNamespace.Templates

<html>

<head>
    <title>Hello @Model</title>
</head>

<body>
    <p>Hello @Model</p>
</body>

</html>

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 instead inherit from MiniRazor.TemplateBase<dynamic> by default, which doesn't offer the same level of type-safety when working with the model.
  • @namespace directive instructs the compiler to put the generated class into the MyNamespace.Templates namespace. If this directive is omitted, 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 as AdditionalFiles and mark it with the IsRazorTemplate="true" attribute:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    
    <AdditionalFiles Include="Templates/TemplateFoo.cshtml" IsRazorTemplate="true" />

    
    <AdditionalFiles Include="Templates/*.cshtml" IsRazorTemplate="true" />
  </ItemGroup>

  

</Project>

Once that's done, you should be able to run dotnet build to build the project and trigger the source generator. Given that the template's file name is TemplateFoo.cshtml, the generated class should be accessible as MyNamespace.Templates.TemplateFoo. To render it, you can call the RenderAsync(...) static method:

// Reference the namespace where the template is located
using MyNamespace.Templates;

// Render the template to 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 from 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 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 and hosts it in a dynamic in-memory assembly. This returns an instance of TemplateDescriptor, which can then be used to render output against a model.

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:

@{
    string GetHtml() => "<p>Hello world!</p>";
}

@GetHtml() // &lt;p&gt;Hello world!&lt;/p&gt;
@Raw(GetHtml()) // <p>Hello world!</p>
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MiniRazor.Compiler:

Package Downloads
MiniRazor

Portable Razor compiler and code generator

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.2 371 9/13/2022
2.2.1 1,790 3/25/2022
2.2.0 1,543 10/7/2021
2.1.4 1,523 7/19/2021
2.1.3 207 7/19/2021
2.1.2 1,773 3/22/2021
2.1.1 230 3/22/2021
2.1.0 190 3/22/2021