Kephas.Templating.Razor 11.1.0

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 Kephas.Templating.Razor --version 11.1.0
NuGet\Install-Package Kephas.Templating.Razor -Version 11.1.0
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="Kephas.Templating.Razor" Version="11.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Kephas.Templating.Razor --version 11.1.0
#r "nuget: Kephas.Templating.Razor, 11.1.0"
#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 Kephas.Templating.Razor as a Cake Addin
#addin nuget:?package=Kephas.Templating.Razor&version=11.1.0

// Install Kephas.Templating.Razor as a Cake Tool
#tool nuget:?package=Kephas.Templating.Razor&version=11.1.0

Razor Templating

Introduction

This package provides the implementation of templating using Razor syntax and *.cshtml files. The cshtml template kind is handled by the RazorTemplatingEngine.

Check the following packages for more information:

Usage

Example of a template: template.cshtml file

@model TemplateModel
<div>Hi @Process(Model.Name)!</div>
@functions {
    private string Process(string str)
    {
        return $"@{str}@";
    }
}

Example of code using this template

  • File based template processing
record TemplateModel(string Name);

// normally you would get the processor injected into the service constructor.
var processor = injector.Resolve<ITemplateProcessor>();

var result = processor.Process(new FileTemplate("C:\\Path\\To\\template.cshtml"), new TemplateModel("Johnny"));
Assert.Equals("<div>Hi @Johnny@!</div>\r\n", result);

// this is a simpler alternative for Razor file templates.
var result = processor.ProcessWithFile("C:\\Path\\To\\template.cshtml", new TemplateModel("Johnny"));
Assert.Equals("<div>Hi @Johnny@!</div>\r\n", result);
  • String based template processing
record TemplateModel(string Name);

// normally you would get the processor injected into the service constructor.
var processor = injector.Resolve<ITemplateProcessor>();

var template = @"
@model TemplateModel
<div>Hi @Process(Model.Name)!</div>
@functions {
    private string Process(string str)
    {
        return $""@{str}@"";
    }
}
";
var result = processor.ProcessWithRazor(template, new TemplateModel("Johnny"));
Assert.Equals("<div>Hi @Johnny@!</div>\r\n", result);

Fine tuning the template processing

By default, the following directives are supported by the templating engine:

  • Namespace
  • Functions
  • Inherits
  • Section
  • Model

For further engine builder configuration, this can be achieved in two ways:

  • Provide a configuration lambda expression when invoking the processing.
    • This is a very handy solution but it is not scalable. If there are a lot of templates to process it is error prone to specify the configuration with each processing invocation.
  • Use a ITemplatingProcessingBehavior implementation.
    • This is an advanced solution. Override the BeforeProcessAsync method and add the engine configuration here. Although it has a more general character, the changes here can only be overwritten either by another behavior with less priority or with an explicit [Override] annotation.

Example with lambda configuration

var processor = injector.Resolve<ITemplateProcessor>();
var result = await processor.ProcessAsync(
    new FileTemplate("C:\\Path\\To\\template.cshtml"),
    new TemplateModel("Johnny"),
    ctx => {
        PageDirective.Register(builder);
    });

Assert.Equals("<div>Hi @Johnny@!</div>\r\n", result);

Example with a template processing behavior

[TemplateKind(RazorTemplatingEngine.Cshtml)]
public class AddPageTemplateProcessingBehavior : ITemplateProcessingBehavior
{
    /// <summary>
    /// Interception invoked before the template is processed.
    /// It adds also the PageDirective to the engine.
    /// </summary>
    /// <param name="processingContext">Information describing the processing.</param>
    /// <param name="token">The cancellation token.</param>
    /// <returns>
    /// The asynchronous result.
    /// </returns>
    public Task BeforeProcessAsync(ITemplateProcessingContext processingContext, CancellationToken token)
    {
        var existingConfig = processingContext.ConfigureEngine();
        processingContext.ConfigureEngine(engine =>
        {
            existingConfig?.Invoke(engine);
            PageDirective.Register(engine);
        });

        return Task.CompletedTask;
    }
}

Additional services

All the enumerated services provide a default implementation which can be overridden if required. However, this should be rarely needed, instead configure the template generation

IMetadataReferenceManager

This internal service is used to provide MetadataReferences out of a list of assemblies.

IRazorProjectFileSystemProvider

Based on the template and the processing context, this service should provide a RazorProjectFileSystem used by the renderer.

The default implementation SimpleRazorProjectFileSystemProvider uses a simple strategy which handles a single template file without any additional template references.

IRazorProjectEngineFactory

Creates the Razor project engine for the provided file system and with the given context.

IRazorPageGenerator

Generates the C# code based on the provided project engine and item.

IRazorPageCompiler

Compiles the template with model type T and returns a ICompiledRazorPage<T> wrapped into an operation result.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
11.1.0 468 4/13/2022
11.1.0-dev.4 124 4/6/2022
11.1.0-dev.3 107 3/30/2022
11.1.0-dev.2 115 3/23/2022
11.1.0-dev.1 108 3/23/2022
11.0.0 420 3/11/2022
11.0.0-dev.7 117 3/7/2022
11.0.0-dev.6 113 2/28/2022
11.0.0-dev.5 112 2/26/2022
11.0.0-dev.4 119 2/24/2022
11.0.0-dev.3 119 2/23/2022
11.0.0-dev.2 118 2/18/2022