{"id":13570583,"url":"https://github.com/Antaris/RazorEngine","last_synced_at":"2025-04-04T07:31:43.906Z","repository":{"id":1561113,"uuid":"1973880","full_name":"Antaris/RazorEngine","owner":"Antaris","description":"Open source templating engine based on Microsoft's Razor parsing engine","archived":false,"fork":false,"pushed_at":"2021-05-11T16:32:23.000Z","size":33900,"stargazers_count":2151,"open_issues_count":178,"forks_count":582,"subscribers_count":149,"default_branch":"master","last_synced_at":"2025-03-29T20:00:46.358Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://antaris.github.io/RazorEngine","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Antaris.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-06-29T18:18:33.000Z","updated_at":"2025-03-28T21:59:21.000Z","dependencies_parsed_at":"2022-07-17T14:19:21.584Z","dependency_job_id":null,"html_url":"https://github.com/Antaris/RazorEngine","commit_stats":null,"previous_names":[],"tags_count":78,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Antaris%2FRazorEngine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Antaris%2FRazorEngine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Antaris%2FRazorEngine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Antaris%2FRazorEngine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Antaris","download_url":"https://codeload.github.com/Antaris/RazorEngine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247138749,"owners_count":20890095,"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":[],"created_at":"2024-08-01T14:00:53.428Z","updated_at":"2025-04-04T07:31:41.626Z","avatar_url":"https://github.com/Antaris.png","language":"C#","readme":"# RazorEngine\n\n[![Join the chat at https://gitter.im/Antaris/RazorEngine](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Antaris/RazorEngine?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\nThis project is searching for new maintainers, so if you want to help write on gitter or start sending PRs :)\n\n*latest* documentation available on https://antaris.github.io/RazorEngine/.\n\n\n## Build status\n\nDevelop Branch (`master`)\n\n[![Build Status](https://travis-ci.org/Antaris/RazorEngine.svg?branch=master)](https://travis-ci.org/Antaris/RazorEngine)\n[![Build status](https://ci.appveyor.com/api/projects/status/39bi38wonhwolrgy/branch/master?svg=true)](https://ci.appveyor.com/project/Antaris/razorengine/branch/master)\n\nRelease Branch (`releases`)\n\n[![Build Status](https://travis-ci.org/Antaris/RazorEngine.svg?branch=releases)](https://travis-ci.org/Antaris/RazorEngine)\n[![Build status](https://ci.appveyor.com/api/projects/status/39bi38wonhwolrgy/branch/releases?svg=true)](https://ci.appveyor.com/project/Antaris/razorengine/branch/releases)\n\n\n## Quickstart\n\nFirst install the nuget package (\u003e=3.5.0).\n\n\tInstall-Package RazorEngine\n\nA templating engine built on Microsoft's Razor parsing engine, RazorEngine allows you to use Razor syntax to build dynamic templates.\nYou can find an introduction [here](http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-%28c%29).\nAll you need to do is use the static `Engine` class (the `Engine.Razor` instance) in the 'RazorEngine' namespace:\n\n```csharp\nusing RazorEngine;\nusing RazorEngine.Templating; // For extension methods.\n\nstring template = \"Hello @Model.Name, welcome to RazorEngine!\";\nvar result =\n\tEngine.Razor.RunCompile(template, \"templateKey\", null, new { Name = \"World\" });\n```\n\n\u003e The `RunCompile` method used here is an extension method and you need to open the `RazorEngine.Templating` namespace.\n\nThe `\"templateKey\"` must be unique and after running the above example you can re-run the cached template with this key.\n\n```csharp\n// using RazorEngine.Templating; // Dont forget to include this.\nvar result =\n\tEngine.Razor.Run(\"templateKey\", null, new { Name = \"Max\" });\n```\n\nThe null parameter is the `modelType` and `null` in this case means we use `dynamic` as the type of the model.\nYou can use a static model as well by providing a type object.\n\n```csharp\n// using RazorEngine.Templating; // Dont forget to include this.\nvar result =\n\tEngine.Razor.RunCompile(\"templateKey\", typeof(Person), new Person { Name = \"Max\" });\n```\n\nNote that we now re-compile the model with a different type. \nWhen you do not run the same template a lot of times (like several 1000 times), compiling uses the most time.\nSo the benefit you get from a static type will most likely not compensate the additional compile time.\nTherefore you should either stick to one type for a template (best of both worlds) or just use (the slower) `dynamic` (`null`).\nYou can specify the `modelType` of a template with the `@model` directive. \nWhen you do this the `modelType` parameter is ignored, but you should use the same type instance (or `null`) \non every call to prevent unnecessary re-compilations because of type mismatches in the caching layer.\n\n## Configuration\n\nYou can configure RazorEngine with the `TemplateServiceConfiguration` class.\n\n```csharp\nvar config = new TemplateServiceConfiguration();\n// .. configure your instance\n\nvar service = RazorEngineService.Create(config);\n```\n\nIf you want to use the static `Engine` class with this new configuration:\n\n```csharp\nEngine.Razor = service;\n```\n\n\n### General Configuration\n\nBy default RazorEngine is configured to encode using Html. \nThis supports the majority of users but with some configuration changes you can also set it to encode using Raw format \nwhich is better suited for templates that generate things like javascript, php, C# and others.\n\n```csharp\nconfig.Language = Language.VisualBasic; // VB.NET as template language.\nconfig.EncodedStringFactory = new RawStringFactory(); // Raw string encoding.\nconfig.EncodedStringFactory = new HtmlEncodedStringFactory(); // Html encoding.\n```\n\n### Debugging\n\nOne thing you might want to enable is the debugging feature:\n\n```csharp\nconfig.Debug = true;\n```\n\nWhen `Debug` is true you can straight up debug into the generated code. \nRazorEngine also supports debugging directly into the template files (normally `.cshtml` files).\nAs you might see in the above code there is no file to debug into.\nTo provide RazorEngine with the necessary information you need to tell where the file can be found:\n\n```csharp\n// using RazorEngine.Templating; // Dont forget to include this.\nstring template = \"Hello @Model.Name, welcome to RazorEngine!\";\nstring templateFile = \"C:/mytemplate.cshtml\"\nvar result =\n\tEngine.Razor.RunCompile(new LoadedTemplateSource(template, templateFile), \"templateKey\", null, new { Name = \"World\" });\n```\n\nThis time when debugging the template you will jump right into the template file.\n\n### Set a template manager\n\nThe API is designed around the idea that you do not have the templates sitting around in the source code \n(while you can do that as seen above).\nThe main interface to provide RazorEngine with templates is the `ITemplateManager` interface.\t\nYou should either pick one of the available implementations or write your own.\nSee [TemplateManager and Caching documentation](http://antaris.github.io/RazorEngine/TemplateManager.html) for details.\n\n## Temporary files\n\nRazorEngine tries hard to delete the temporary files it creates, but this is not always possible.\nThis is especially true if you run RazorEngine from the default `AppDomain`.\nRazorEngine will warn you in this situation by writing to the stderr. \nOne way to switch into a new AppDomain is to use the following snippet:\n\n```csharp\nstatic int Main(string[] args)\n{\n    if (AppDomain.CurrentDomain.IsDefaultAppDomain())\n    {\n        // RazorEngine cannot clean up from the default appdomain...\n        Console.WriteLine(\"Switching to secound AppDomain, for RazorEngine...\");\n        AppDomainSetup adSetup = new AppDomainSetup();\n        adSetup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;\n        var current = AppDomain.CurrentDomain;\n        // You only need to add strongnames when your appdomain is not a full trust environment.\n        var strongNames = new StrongName[0];\n\n        var domain = AppDomain.CreateDomain(\n            \"MyMainDomain\", null,\n            current.SetupInformation, new PermissionSet(PermissionState.Unrestricted),\n            strongNames);\n        var exitCode = domain.ExecuteAssembly(Assembly.GetExecutingAssembly().Location);\n        // RazorEngine will cleanup. \n        AppDomain.Unload(domain);\n        return exitCode;\n    }\n    // Continue with your code.\n}\n```\n\nDepending on your scenario you probably need to edit it to your needs.\nNote that you need to `Unload` the domain to trigger cleanup.\n\nFor the following scenario:\n\n * Your templates are limited in number.\n * You fully trust your templates / don't need isolation.\n * You don't need any kind of debugging support.\n * Your templates do not change in runtime.\n\nYou can use `config.DisableTempFileLocking = true` as well. This will work in any AppDomain (including the default one).\nTo remove the RazorEngine warnings you can additionally use `config.CachingProvider = new DefaultCachingProvider(t =\u003e {})`.\n\nSee also https://github.com/Antaris/RazorEngine/issues/244 for more details.\n\n\n## More\n\nOn the right side you can find links to advanced topics and additional [documentation](http://antaris.github.io/RazorEngine/).\nYou should definitely read \"About Razor\" and \"Template basics\".\n\n","funding_links":[],"categories":["C#","C# #","Libraries","Template","C\\#","Template Engine","模板引擎"],"sub_categories":["Templating"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAntaris%2FRazorEngine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAntaris%2FRazorEngine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAntaris%2FRazorEngine/lists"}