Dartk.RazorGen 0.2.2

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package Dartk.RazorGen --version 0.2.2
NuGet\Install-Package Dartk.RazorGen -Version 0.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.
<PackageReference Include="Dartk.RazorGen" Version="0.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dartk.RazorGen --version 0.2.2
#r "nuget: Dartk.RazorGen, 0.2.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Dartk.RazorGen as a Cake Addin
#addin nuget:?package=Dartk.RazorGen&version=0.2.2

// Install Dartk.RazorGen as a Cake Tool
#tool nuget:?package=Dartk.RazorGen&version=0.2.2

RazorGen

A C# source generator that renders Razor templates.

Warning: The source generator requires .NET 7 runtime installed and dotnet command available. See Source generation section for more information about the source generation process.

Info: If the project targets .NET Standard 2.0, then the project's references can be used in Razor templates.

Installation

dotnet add package Dartk.RazorGen

To avoid propagating dependency on the package set the option PrivateAssets="all" in the project file:


<ItemGroup>
    <PackageReference Include="Dartk.RazorGen" Version="0.2.2" PrivateAssets="All" />
</ItemGroup>

Include razor template files with .razor extension to the project as AdditionalFiles. For example, to render all razor templates in the project add this to the project file:


<ItemGroup>
    <AdditionalFiles Include="**/*.razor" />
</ItemGroup>

A complete example is presented below.

Source generation

Razor engine does not render a template directly, instead it generates a C# class that has a method that returns a rendered output. In order to render a template, an intermediate library with Razor generated classes needs to be compiled.

The source generator passes all found .razor (included to the project as AdditionalFiles) templates to the Razor engine, which generates C# classes that render templates. Those classes are compiled into a temporary intermediate library.

Source generators target .NET Standard 2.0 which does not support assembly unloading. In order to prevent memory leak by repeated assembly loading, the intermediate library is called in a separate process by an external .NET 6 executable.

If the project that uses the source generator targets .NET Standard 2.0, then the project's references will be referenced by the intermediate library.

Saving generated files

To save the generated source files set properties EmitCompilerGeneratedFiles and CompilerGeneratedFilesOutputPath in the project file:


<PropertyGroup>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    
    <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedFiles
    </CompilerGeneratedFilesOutputPath>
</PropertyGroup>

Example

Create a new console C# project:

dotnet new Example.Netstandard2_0

Install the package Dartk.RazorGen and set the property PrivateAssets="All" by editing the project file Example.csproj:


<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <LangVersion>latest</LangVersion>
    </PropertyGroup>

    <ItemGroup>
        
        <PackageReference Include="Dartk.RazorGen" Version="0.2.2"
                          PrivateAssets="All" />

        
        <PackageReference Include="Scriban" Version="5.7.0" />
    </ItemGroup>

    
    <ItemGroup>
        <AdditionalFiles Include="**\*.razor" />
    </ItemGroup>
</Project>

Create a file RazorScribanMadness.razor:

namespace Example.Netstandard2_0;

// 'partial' is used for JetBrains Rider, it's linter thinks that a '.razor' file declares
// a class, and will treat the generated code as second declaration, highlighting errors
// in places where the class is being used.
public static partial class RazorScribanMadness
{
    @using Scriban
    @{
        @:public static string Why() => "@(RenderScriban())";

        string RenderScriban()
        {
            var template = Template.Parse("Because {{ reason }}!");
            return template.Render(new { reason = "you can" });
        }
    }
}

The template above will generate following code:

namespace Example.Netstandard2_0;

// 'partial' is used for JetBrains Rider, it's linter thinks that a '.razor' file declares
// a class, and will treat the generated code as second declaration, highlighting errors
// in places where the class is being used.
public static partial class RazorScribanMadness
{
        public static string Why() => "Because you can!";
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.2 187 6/30/2023