Need help with RazorEngineCore?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

adoconnection
254 Stars 44 Forks MIT License 133 Commits 14 Opened issues

Description

.NET5 Razor Template Engine

Services available

!
?

Need anything else?

Contributors list

# 195,985
C#
extract...
.NET
7zip
89 commits
# 263,486
windows...
azure-e...
Azure
C
3 commits
# 5,986
c-sharp
linq
logging...
netstan...
3 commits
# 87,968
HTML
C#
session...
kibana
2 commits
# 560,766
C
Shell
Common ...
dotnet-...
1 commit
# 614,553
C
Shell
C#
dotnet-...
1 commit

RazorEngineCore

.NET5 Razor Template Engine. No legacy code. * .NET 5.0 * .NET Standard 2.0 * .NET Framework 4.7.2 * Windows / Linux * Publish as single file supported

NuGet NuGet Gitter

Every single star makes maintainer happy! ⭐

NuGet

Install-Package RazorEngineCore

Articles

Wiki

Extensions

  • wdcossey/RazorEngineCore.Extensions
    • HTML values encoded by default (See issue #65 and @Raw)
    • Template precompiling
    • Direct model usage without RazorEngineTemplateBase
      cs
      template.Run(object model = null)
      template.RunAsync(object model = null)
      template.Run(TModel model = null)
      template.RunAsync(TModel model = null)
      
      ## :boom: HTML Safety RazorEngineCore is not HTML safe by default. \ It can be easily turned on: see #65 and @Raw

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> template = razorEngine.Compile>(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 ```cs 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 template1 = RazorEngineCompiledTemplate.LoadFromFile("myTemplate.dll");
IRazorEngineCompiledTemplate template2 = RazorEngineCompiledTemplate.LoadFromStream(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 TemplateCache = new ConcurrentDictionary();
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: ``` @{ RecursionTest(3); }

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

LEVEL: @level
@{ RecursionTest(level - 1); }

} }

output:
LEVEL: 3
LEVEL: 2
LEVEL: 1
```

Helpers and custom members

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

IRazorEngine razorEngine = new RazorEngine(); IRazorEngineCompiledTemplate template = razorEngine.Compile(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.7.1
  • 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

Supported by

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.