All Projects → adoconnection → Razorenginecore

adoconnection / Razorenginecore

Licence: mit
.NET5 Razor Template Engine

Projects that are alternatives of or similar to Razorenginecore

Coravel
Near-zero config .NET Core micro-framework that makes advanced application features like Task Scheduling, Caching, Queuing, Event Broadcasting, and more a breeze!
Stars: ✭ 1,989 (+1166.88%)
Mutual labels:  dotnet-core
Ether.network
https://github.com/Eastrall/Sylver
Stars: ✭ 147 (-6.37%)
Mutual labels:  dotnet-core
Appmetrics
App Metrics is an open-source and cross-platform .NET library used to record and report metrics within an application.
Stars: ✭ 1,986 (+1164.97%)
Mutual labels:  dotnet-core
Sevenzipextractor
C# wrapper for 7z.dll
Stars: ✭ 143 (-8.92%)
Mutual labels:  dotnet-core
Ben.demystifier
High performance understanding for stack traces (Make error logs more productive)
Stars: ✭ 2,142 (+1264.33%)
Mutual labels:  dotnet-core
Designpatternsincsharp
Samples associated with Pluralsight design patterns in c# courses.
Stars: ✭ 149 (-5.1%)
Mutual labels:  dotnet-core
Entityframeworkcore.cacheable
EntityFrameworkCore second level cache
Stars: ✭ 138 (-12.1%)
Mutual labels:  dotnet-core
Magazine Website
🐭 A magazine website (using .NET Core, ASP.NET Core, EF Core) with DDD, CQRS, microservices, asynchronous programming applied...
Stars: ✭ 155 (-1.27%)
Mutual labels:  dotnet-core
Supersafebank
Sample Event Sourcing implementation with .NET Core
Stars: ✭ 142 (-9.55%)
Mutual labels:  dotnet-core
Nanui
NanUI is an open source .NET project for .NET / .NET Core developers who want to use front-end technologies such as HTML5 / CSS3 to build user interfaces for Windows Form applications.
Stars: ✭ 2,090 (+1231.21%)
Mutual labels:  dotnet-core
Practical Dapr
A full-stack .NET microservices build on Dapr and Tye
Stars: ✭ 140 (-10.83%)
Mutual labels:  dotnet-core
Skclusive.blazor.samples
Skclusive.Blazor.Samples
Stars: ✭ 144 (-8.28%)
Mutual labels:  dotnet-core
Dotnetcore
.NET 5 Nuget Packages.
Stars: ✭ 146 (-7.01%)
Mutual labels:  dotnet-core
Glob
A C# Glob library for .NET and .NET Core.
Stars: ✭ 142 (-9.55%)
Mutual labels:  dotnet-core
Modernarchitectureshop
The Microservices Online Shop is an application with a modern software architecture that is cleanly designed and based on.NET lightweight technologies. The shop has two build variations. The first variant is the classic Microservices Architectural Style. The second one is with Dapr. Dapr has a comprehensive infrastructure for building highly decoupled Microservices; for this reason, I am using Dapr to achieve the noble goal of building a highly scalable application with clean architecture and clean code.
Stars: ✭ 154 (-1.91%)
Mutual labels:  dotnet-core
Orleans.clustering.kubernetes
Orleans Membership provider for Kubernetes
Stars: ✭ 140 (-10.83%)
Mutual labels:  dotnet-core
Nager.amazonproductadvertising
.NET Amazon Product Advertising Client
Stars: ✭ 147 (-6.37%)
Mutual labels:  dotnet-core
Piggyvault
Family finance management app.
Stars: ✭ 152 (-3.18%)
Mutual labels:  dotnet-core
Nhibernate Core
NHibernate Object Relational Mapper
Stars: ✭ 1,918 (+1121.66%)
Mutual labels:  dotnet-core
Netbarcode
Barcode generation library written in C# and .NET Standard 2
Stars: ✭ 149 (-5.1%)
Mutual labels:  dotnet-core

RazorEngineCore

.NET5 Razor Template Engine. No legacy code.

  • .NET 5.0
  • .NET Standard 2.0
  • .NET Framework 4.7.2
  • Windows / Linux

NuGet NuGet Gitter

Every single star makes maintainer happy! ⭐

NuGet

Install-Package RazorEngineCore

Articles

Wiki

Extensions

Examples

Basic usage

IRazorEngine razorEngine = new RazorEngine();
IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Name");

string result = template.Run(new
{
    Name = "Alexander"
});

Console.WriteLine(result);

Strongly typed model

IRazorEngine razorEngine = new RazorEngine();
string templateText = "Hello @Model.Name";

// yeah, heavy definition
IRazorEngineCompiledTemplate<RazorEngineTemplateBase<TestModel>> template = razorEngine.Compile<RazorEngineTemplateBase<TestModel>>(templateText);

string result = template.Run(instance =>
{
    instance.Model = new TestModel()
    {
        Name = "Hello",
        Items = new[] {3, 1, 2}
    };
});

Console.WriteLine(result);

Save / Load compiled templates

Most expensive task is to compile template, you should not compile template every time you need to run it

IRazorEngine razorEngine = new RazorEngine();
IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Name");

// save to file
template.SaveToFile("myTemplate.dll");

//save to stream
MemoryStream memoryStream = new MemoryStream();
template.SaveToStream(memoryStream);
IRazorEngineCompiledTemplate template1 = RazorEngineCompiledTemplate.LoadFromFile("myTemplate.dll");
IRazorEngineCompiledTemplate template2 = RazorEngineCompiledTemplate.LoadFromStream(myStream);
IRazorEngineCompiledTemplate<MyBase> template1 = RazorEngineCompiledTemplate<MyBase>.LoadFromFile<MyBase>("myTemplate.dll");
IRazorEngineCompiledTemplate<MyBase> template2 = RazorEngineCompiledTemplate<MyBase>.LoadFromStream<MyBase>(myStream);

Caching

RazorEngineCore is not responsible for caching. Each team and project has their own caching frameworks and conventions therefore making it impossible to have builtin solution for all possible needs.

If you dont have one, use following static ConcurrentDictionary example as a simplest thread safe solution.

private static ConcurrentDictionary<int, IRazorEngineCompiledTemplate> TemplateCache = new ConcurrentDictionary<int, IRazorEngineCompiledTemplate>();
private string RenderTemplate(string template, object model)
{
    int hashCode = template.GetHashCode();

    IRazorEngineCompiledTemplate compiledTemplate = TemplateCache.GetOrAdd(hashCode, i =>
    {
        RazorEngine razorEngine = new RazorEngine();
        return razorEngine.Compile(Content);
    });

    return compiledTemplate.Run(model);
}

Template functions

ASP.NET Core way of defining template functions:

<area>
    @{ RecursionTest(3); }
</area>

@{
  void RecursionTest(int level)
  {
	if (level <= 0)
	{
		return;
	}

	<div>LEVEL: @level</div>
	@{ RecursionTest(level - 1); }
  }
}

output:

<div>LEVEL: 3</div>
<div>LEVEL: 2</div>
<div>LEVEL: 1</div>

Helpers and custom members

string content = @"Hello @A, @B, @Decorator(123)";

IRazorEngine razorEngine = new RazorEngine();
IRazorEngineCompiledTemplate<CustomModel> template = razorEngine.Compile<CustomModel>(content);

string result = template.Run(instance =>
{
    instance.A = 10;
    instance.B = "Alex";
});

Console.WriteLine(result);
public class CustomModel : RazorEngineTemplateBase
{
    public int A { get; set; }
    public string B { get; set; }

    public string Decorator(object value)
    {
        return "-=" + value + "=-";
    }
}

Referencing assemblies

Keep your templates as simple as possible, if you need to inject "unusual" assemblies most likely you are doing it wrong. Writing @using System.IO in template will not reference System.IO assembly, use builder to manually reference it.

IRazorEngine razorEngine = new RazorEngine();
IRazorEngineCompiledTemplate compiledTemplate = razorEngine.Compile(templateText, builder =>
{
    builder.AddAssemblyReferenceByName("System.Security"); // by name
    builder.AddAssemblyReference(typeof(System.IO.File)); // by type
    builder.AddAssemblyReference(Assembly.Load("source")); // by reference
});

string result = compiledTemplate.Run(new { name = "Hello" });

Credits

This package is inspired by Simon Mourier SO post

Changelog

  • 2021.3.1
    • fixed NET5 publish as single file (thanks @jddj007-hydra)
    • AnonymousTypeWrapper array handling fix
    • System.Collections referenced by default
    • Microsoft.AspNetCore.Razor.Language 3.1.8 -> 5.0.3
    • Microsoft.CodeAnalysis.CSharp 3.7.0 -> 3.8.0
  • 2020.10.1
    • Linux fix for #34
    • Microsoft.AspNetCore.Razor.Language 3.1.5 -> 3.1.8
    • Microsoft.CodeAnalysis.CSharp 3.6.0 -> 3.7.0
  • 2020.9.1
    • .NET 4.7.2 support (thanks @krmr)
  • 2020.6.1
    • Reference assemblies by Metadata (thanks @Merlin04)
    • Expose GeneratedCode in RazorEngineCompilationException
    • Microsoft.AspNetCore.Razor.Language 3.1.4 -> 3.1.5
  • 2020.5.2
    • IRazorEngineTemplate interface
    • RazorEngineTemplateBase methods go virtual
  • 2020.5.1
    • Async methods (thanks @wdcossey)
    • Microsoft.AspNetCore.Razor.Language 3.1.1 -> 3.1.4
    • Microsoft.CodeAnalysis.CSharp 3.4.0 -> 3.6.0
  • 2020.3.3
    • Model with generic type arguments compiling fix
  • 2020.3.2
    • External assembly referencing
    • Linq included by default
  • 2020.3.1
    • In attribute rendering fix #4
  • 2020.2.4
    • Null values in model correct handling
    • Null model fix
    • Netstandard2 insted of netcore3.1
  • 2020.2.3
    • Html attribute rendering fix
    • Html attribute rendering tests
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].