{"id":19099962,"url":"https://github.com/adoconnection/RazorEngineCore","last_synced_at":"2025-04-18T17:32:25.539Z","repository":{"id":40850757,"uuid":"239130043","full_name":"adoconnection/RazorEngineCore","owner":"adoconnection","description":".NET6 Razor Template Engine","archived":false,"fork":false,"pushed_at":"2024-06-11T02:38:32.000Z","size":220,"stargazers_count":581,"open_issues_count":17,"forks_count":86,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-11-01T21:07:08.563Z","etag":null,"topics":["csharp","dotnet-core","net5","net6","netcore31","razorengine","template-engine"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adoconnection.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-08T12:37:39.000Z","updated_at":"2024-11-01T16:35:08.000Z","dependencies_parsed_at":"2024-02-23T14:47:03.415Z","dependency_job_id":"30c4d308-cf13-40d7-b134-d0c0b3657423","html_url":"https://github.com/adoconnection/RazorEngineCore","commit_stats":{"total_commits":137,"total_committers":12,"mean_commits":"11.416666666666666","dds":0.4890510948905109,"last_synced_commit":"c6ff4880e6a31332d16006e2fdc043f10ebbfa78"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adoconnection%2FRazorEngineCore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adoconnection%2FRazorEngineCore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adoconnection%2FRazorEngineCore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adoconnection%2FRazorEngineCore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adoconnection","download_url":"https://codeload.github.com/adoconnection/RazorEngineCore/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223783107,"owners_count":17201903,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet-core","net5","net6","netcore31","razorengine","template-engine"],"created_at":"2024-11-09T03:52:24.496Z","updated_at":"2024-11-09T03:52:48.043Z","avatar_url":"https://github.com/adoconnection.png","language":"C#","readme":"# RazorEngineCore\n.NET6 Razor Template Engine. No legacy code. No breaking changes.\n* .NET 6.0\n* .NET 5.0\n* .NET Standard 2.0\n* .NET Framework 4.7.2\n* Windows / Linux\n* Publish as single file supported\n* Thread safe\n* Template debugging\n\n[![NuGet](https://img.shields.io/nuget/dt/RazorEngineCore.svg?style=flat-square)](https://www.nuget.org/packages/RazorEngineCore)\n[![NuGet](https://img.shields.io/nuget/v/RazorEngineCore.svg?style=flat-square)](https://www.nuget.org/packages/RazorEngineCore)\n[![Gitter](https://img.shields.io/gitter/room/RazorEngineCore/community?style=flat-square)](https://gitter.im/RazorEngineCore/community?utm_source=badge\u0026utm_medium=badge\u0026utm_content=badge)\n\n\n\nEvery single star makes maintainer happy! ⭐\n\n## NuGet\n```\nInstall-Package RazorEngineCore\n```\n\n## Feature requests\n✅ Feel free to create new issues and vote for existing ones!\n\n\n## Articles\n* [🚀 Piotr Bakun: How to make PDF invoice with Playwright.NET](https://bakson.dev/2022/11/10/razor-engine-core.html)\n* [🚀 Christian Schou: How to send email with MailKit](https://blog.christian-schou.dk/send-emails-with-asp-net-core-with-mailkit/)\n* [🚀 Dave Glick: Difference between RazorEngineCore, RazorLight and Statiq, Send email using SendGrid API](https://www.twilio.com/blog/render-emails-using-razor-templating)\n* [🧑‍🎓 Alexander Selishchev: Building String Razor Template Engine with Bare Hands](https://www.codeproject.com/Articles/5260233/Building-String-Razor-Template-Engine-with-Bare-Ha)\n* [📕 Razor syntax reference](https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-6.0)\n \n## Wiki\n* [Package comparison: RazorEngineCore / RazorLight / RazorEngine.NetCore](https://github.com/adoconnection/RazorEngineCore/wiki/Package-comparison)\n* [Strongly typed model](https://github.com/adoconnection/RazorEngineCore/wiki/Strongly-typed-model)\n* [@Include and @Layout](https://github.com/adoconnection/RazorEngineCore/wiki/@Include-and-@Layout)\n* [@Raw](https://github.com/adoconnection/RazorEngineCore/wiki/@Raw)\n* [@Inject and referencing other assemblies](https://github.com/adoconnection/RazorEngineCore/wiki/@Inject-and-referencing-other-assemblies)\n* [Switch from RazorEngine cshtml templates](https://github.com/adoconnection/RazorEngineCore/wiki/Switch-from-RazorEngine-cshtml-templates)\n* [Azure Functions FileNotFoundException workaround](https://github.com/adoconnection/RazorEngineCore/wiki/Azure-Functions-FileNotFoundException-workaround)\n* [@Html implementation example](https://github.com/adoconnection/RazorEngineCore/wiki/@Html-implementation-example)\n* [Debugging](https://github.com/adoconnection/RazorEngineCore/wiki/Debugging)\n  \n\n## Extensions\n* [wdcossey/RazorEngineCore.Extensions](https://github.com/wdcossey/RazorEngineCore.Extensions)\n\t- HTML values encoded by default (See [issue #65](https://github.com/adoconnection/RazorEngineCore/issues/65) and [@Raw](https://github.com/adoconnection/RazorEngineCore/wiki/@Raw))\n\t- Template precompiling\n\t- Direct model usage without RazorEngineTemplateBase\n\t```cs\n\ttemplate.Run(object model = null)\n\ttemplate.RunAsync(object model = null)\n\ttemplate.Run\u003cTModel\u003e(TModel model = null)\n\ttemplate.RunAsync\u003cTModel\u003e(TModel model = null)\n\t```\n## :boom: HTML Safety\nRazorEngineCore is not HTML safe by default. \\\nIt can be easily turned on: see [#65](https://github.com/adoconnection/RazorEngineCore/issues/65) and [@Raw](https://github.com/adoconnection/RazorEngineCore/wiki/@Raw)\n\n## Examples\n\n#### Basic usage\n```cs\nIRazorEngine razorEngine = new RazorEngine();\nIRazorEngineCompiledTemplate template = razorEngine.Compile(\"Hello @Model.Name\");\n\nstring result = template.Run(new\n{\n    Name = \"Alexander\"\n});\n\nConsole.WriteLine(result);\n```\n\n#### Strongly typed model\n```cs\nIRazorEngine razorEngine = new RazorEngine();\nstring templateText = \"Hello @Model.Name\";\n\n// yeah, heavy definition\nIRazorEngineCompiledTemplate\u003cRazorEngineTemplateBase\u003cTestModel\u003e\u003e template = razorEngine.Compile\u003cRazorEngineTemplateBase\u003cTestModel\u003e\u003e(templateText);\n\nstring result = template.Run(instance =\u003e\n{\n    instance.Model = new TestModel()\n    {\n        Name = \"Hello\",\n        Items = new[] {3, 1, 2}\n    };\n});\n\nConsole.WriteLine(result);\n```\n\n#### Debugging\nCompile template with ```IncludeDebuggingInfo()``` option and call ```EnableDebugging()``` before running template. If template was compiled with ```IncludeDebuggingInfo()``` option, saving and loading will keep original template source code and pdb.\n```cs\nIRazorEngineCompiledTemplate template2 = razorEngine.Compile(templateText, builder =\u003e\n{\n    builder.IncludeDebuggingInfo();\n});\n\ntemplate2.EnableDebugging(); // optional path to output directory\n\nstring result = template2.Run(new\n{\n    Title = \"Welcome\"\n});\n```\nPlace ```@{ Breakpoint(); }``` anywhere in template to stop at.\n\n\n#### Save / Load compiled templates\nMost expensive task is to compile template, you should not compile template every time you need to run it\n```cs\nIRazorEngine razorEngine = new RazorEngine();\nIRazorEngineCompiledTemplate template = razorEngine.Compile(\"Hello @Model.Name\");\n\n// save to file\ntemplate.SaveToFile(\"myTemplate.dll\");\n\n//save to stream\nMemoryStream memoryStream = new MemoryStream();\ntemplate.SaveToStream(memoryStream);\n```\n\n```cs\nIRazorEngineCompiledTemplate template1 = RazorEngineCompiledTemplate.LoadFromFile(\"myTemplate.dll\");\nIRazorEngineCompiledTemplate template2 = RazorEngineCompiledTemplate.LoadFromStream(myStream);\n```\n\n```cs\nIRazorEngineCompiledTemplate\u003cMyBase\u003e template1 = RazorEngineCompiledTemplate\u003cMyBase\u003e.LoadFromFile\u003cMyBase\u003e(\"myTemplate.dll\");\nIRazorEngineCompiledTemplate\u003cMyBase\u003e template2 = RazorEngineCompiledTemplate\u003cMyBase\u003e.LoadFromStream\u003cMyBase\u003e(myStream);\n```\n\n#### Caching\nRazorEngineCore 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. \n\nIf you dont have one, use following static ConcurrentDictionary example as a simplest thread safe solution.\n\n```cs\nprivate static ConcurrentDictionary\u003cint, IRazorEngineCompiledTemplate\u003e TemplateCache = new ConcurrentDictionary\u003cint, IRazorEngineCompiledTemplate\u003e();\n```\n\n```cs\nprivate string RenderTemplate(string template, object model)\n{\n    int hashCode = template.GetHashCode();\n\n    IRazorEngineCompiledTemplate compiledTemplate = TemplateCache.GetOrAdd(hashCode, i =\u003e\n    {\n        RazorEngine razorEngine = new RazorEngine();\n        return razorEngine.Compile(Content);\n    });\n\n    return compiledTemplate.Run(model);\n}\n```\n\n#### Template functions\nASP.NET Core way of defining template functions:\n```\n\u003carea\u003e\n    @{ RecursionTest(3); }\n\u003c/area\u003e\n\n@{\n  void RecursionTest(int level)\n  {\n\tif (level \u003c= 0)\n\t{\n\t\treturn;\n\t}\n\n\t\u003cdiv\u003eLEVEL: @level\u003c/div\u003e\n\t@{ RecursionTest(level - 1); }\n  }\n}\n```\noutput:\n```\n\u003cdiv\u003eLEVEL: 3\u003c/div\u003e\n\u003cdiv\u003eLEVEL: 2\u003c/div\u003e\n\u003cdiv\u003eLEVEL: 1\u003c/div\u003e\n```\n\n#### Helpers and custom members\n```cs\nstring content = @\"Hello @A, @B, @Decorator(123)\";\n\nIRazorEngine razorEngine = new RazorEngine();\nIRazorEngineCompiledTemplate\u003cCustomTemplate\u003e template = razorEngine.Compile\u003cCustomTemplate\u003e(content);\n\nstring result = template.Run(instance =\u003e\n{\n    instance.A = 10;\n    instance.B = \"Alex\";\n});\n\nConsole.WriteLine(result);\n```\n```cs\npublic class CustomTemplate : RazorEngineTemplateBase\n{\n    public int A { get; set; }\n    public string B { get; set; }\n\n    public string Decorator(object value)\n    {\n        return \"-=\" + value + \"=-\";\n    }\n}\n```\n\n#### Referencing assemblies\nKeep your templates as simple as possible, if you need to inject \"unusual\" assemblies most likely you are doing it wrong.\nWriting `@using System.IO` in template will not reference System.IO assembly, use builder to manually reference it.\n\n```cs\nIRazorEngine razorEngine = new RazorEngine();\nIRazorEngineCompiledTemplate compiledTemplate = razorEngine.Compile(templateText, builder =\u003e\n{\n    builder.AddAssemblyReferenceByName(\"System.Security\"); // by name\n    builder.AddAssemblyReference(typeof(System.IO.File)); // by type\n    builder.AddAssemblyReference(Assembly.Load(\"source\")); // by reference\n});\n\nstring result = compiledTemplate.Run(new { name = \"Hello\" });\n```\n\n#### Credits\nThis package is inspired by [Simon Mourier SO post](https://stackoverflow.com/a/47756437/267736)\n\n\n#### Changelog\n* 2024.4.1\n\t* Expose RazorProjectEngineBuilder 🚀 #141 (thanks [@cutslikeaknife](https://github.com/ItWorksOnMyMachine))\n \t* Documentation for RazorEngineCompilationOptionsBuilder #140 (thanks [@MichaelCleverdon](https://github.com/MichaelCleverdon))\n \t* More asyncs #138 (thanks [@Adam Kauffman](https://github.com/A9G-Data-Droid))\n* 2023.11.2\n\t* Virtual keyword fixed\n* 2023.11.1\n\t* Template debugging 🚀 PR#111\t(thanks [@shehrozeee](https://github.com/shehrozeee) and [@TheAtomicOption](https://github.com/TheAtomicOption))\n \t* Add CancellationToken overloads to Compile methods PR#134 (thanks [@daviddotcs](https://github.com/daviddotcs))\n  \t* Package license information #133\n  \t* Code cleanup\n\t* ⚠️ Breaking change for @Raw, [update your code from wiki](https://github.com/adoconnection/RazorEngineCore/wiki/@Raw)\n* 2022.8.1\n\t* Proper namespace handling for nested types and types without namespace #113 (thanks [@Kirmiir](https://github.com/Kirmiir))\t\n* 2022.7.6\n\t* Added the option to genereate pdb alongside the assembly which allows debugging the templates.\n* 2022.1.2\n\t* #94 publish as single file fix \t\n* 2022.1.1\n\t* Make private methods in RazorEngine protected and virtual #PR88 (thanks [@wdcossey](https://github.com/wdcossey))\t\n\t* Dictionary bug in anonymous model #91 (thanks [@jddj007-hydra](https://github.com/jddj007-hydra))\t\n\t* Template name fix #PR84 (thanks [@Yazwh0](https://github.com/Yazwh0))\t\n\t* CI for GitHub Actions #PR69 (thanks [@304NotModified](https://github.com/304NotModified))\t\n\t* Added Source Link #PR67 (thanks [@304NotModified](https://github.com/304NotModified))\t\n\t* Microsoft.AspNetCore.Razor.Language 3.1.8 -\u003e 6.0.1\n\t* Microsoft.CodeAnalysis.CSharp 3.7.0 -\u003e 4.0.1\n* 2021.7.1\n\t* Better error messages #PR54 (thanks [@wdcossey](https://github.com/wdcossey))\t\n\t* More asyncs #PR53 (thanks [@wdcossey](https://github.com/wdcossey))\n\t* Strong name for assembly #59 (thanks [@garryxiao](https://github.com/garryxiao))\n* 2021.3.1\n\t* fixed NET5 publish as single file (thanks [@jddj007-hydra](https://github.com/jddj007-hydra))\n\t* AnonymousTypeWrapper array handling fix\n\t* System.Collections referenced by default\n\t* Microsoft.AspNetCore.Razor.Language 3.1.8 -\u003e 5.0.3\n\t* Microsoft.CodeAnalysis.CSharp 3.7.0 -\u003e 3.8.0\n* 2020.10.1\n\t* Linux fix for #34\n\t* Microsoft.AspNetCore.Razor.Language 3.1.5 -\u003e 3.1.8\n\t* Microsoft.CodeAnalysis.CSharp 3.6.0 -\u003e 3.7.0\n* 2020.9.1\n\t* .NET 4.7.2 support (thanks [@krmr](https://github.com/krmr))\n* 2020.6.1\n\t* Reference assemblies by Metadata (thanks [@Merlin04](https://github.com/Merlin04))\n\t* Expose GeneratedCode in RazorEngineCompilationException\n\t* Microsoft.AspNetCore.Razor.Language 3.1.4 -\u003e 3.1.5\n* 2020.5.2\n\t* IRazorEngineTemplate interface \n\t* RazorEngineTemplateBase methods go virtual\n* 2020.5.1\n\t* Async methods (thanks [@wdcossey](https://github.com/wdcossey))\n\t* Microsoft.AspNetCore.Razor.Language 3.1.1 -\u003e 3.1.4\n\t* Microsoft.CodeAnalysis.CSharp 3.4.0 -\u003e 3.6.0\n* 2020.3.3\n\t* Model with generic type arguments compiling fix\n* 2020.3.2\n\t* External assembly referencing\n\t* Linq included by default\n* 2020.3.1\n\t* In attribute rendering fix #4\n* 2020.2.4\n\t* Null values in model correct handling\n\t* Null model fix\n\t* Netstandard2 insted of netcore3.1\n* 2020.2.3\n\t* Html attribute rendering fix\n\t* Html attribute rendering tests\n\n## Supported by\n[\u003cimg src=\"https://259506.selcdn.ru/sites-static/site609131/d7defb1c-34d5-4719-8dd8-5c0395badfeb/d7defb1c-34d5-4719-8dd8-5c0395badfeb-2809062.png\" /\u003e](https://limetime.io)\n","funding_links":[],"categories":["csharp"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadoconnection%2FRazorEngineCore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadoconnection%2FRazorEngineCore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadoconnection%2FRazorEngineCore/lists"}