{"id":13808184,"url":"https://github.com/conficient/BlazorTemplater","last_synced_at":"2025-05-14T02:31:33.874Z","repository":{"id":40244056,"uuid":"357255701","full_name":"conficient/BlazorTemplater","owner":"conficient","description":"A library that generates HTML (e.g. for emails) from Razor Components","archived":false,"fork":false,"pushed_at":"2024-06-10T03:54:49.000Z","size":97,"stargazers_count":150,"open_issues_count":5,"forks_count":18,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-11-01T07:16:05.249Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/conficient.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":"2021-04-12T16:01:02.000Z","updated_at":"2024-10-27T20:14:23.000Z","dependencies_parsed_at":"2024-06-21T17:30:08.540Z","dependency_job_id":"8e2ccbe4-3a40-455b-85d1-036f5cb7f9a6","html_url":"https://github.com/conficient/BlazorTemplater","commit_stats":{"total_commits":35,"total_committers":2,"mean_commits":17.5,"dds":"0.11428571428571432","last_synced_commit":"d712e9463e5b83634b4dc7e7683dd4f6c871dce5"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/conficient%2FBlazorTemplater","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/conficient%2FBlazorTemplater/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/conficient%2FBlazorTemplater/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/conficient%2FBlazorTemplater/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/conficient","download_url":"https://codeload.github.com/conficient/BlazorTemplater/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225273224,"owners_count":17448071,"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-04T01:01:36.916Z","updated_at":"2024-11-19T00:30:37.556Z","avatar_url":"https://github.com/conficient.png","language":"C#","funding_links":[],"categories":["Libraries \u0026 Extensions"],"sub_categories":["Tools \u0026 Utilities"],"readme":"# BlazorTemplater\nA library that generates HTML (e.g. for emails) from [Razor Components](https://docs.microsoft.com/en-us/aspnet/core/blazor/components).\n\n[![Build](https://github.com/conficient/BlazorTemplater/actions/workflows/dotnet-core.yml/badge.svg)](https://github.com/conficient/BlazorTemplater/actions/workflows/dotnet-core.yml) [![Nuget](https://img.shields.io/nuget/dt/blazortemplater?logo=nuget\u0026style=flat-square)](https://www.nuget.org/packages/blazortemplater/)\n\n#### Examples\n\nThe `ComponentRenderer` uses a [fluent interface](https://en.wikipedia.org/wiki/Fluent_interface).\n\nLet's render `MyComponent.razor` as a HTML string.\n```c#\nstring html = new ComponentRenderer\u003cMyComponent\u003e().Render();\n```\n\n\n\u003cdetails\u003e\n \u003csummary\u003e\u003ccode\u003eMyComponent.razor\u003c/code\u003e\u003c/summary\u003e\n            \n```html\n\u003cp\u003eHello from BlazorTemplater!\u003c/p\u003e\n```\n            \n\u003c/details\u003e\n\n**Parameters**\n\nYou can set parameters on a component:\n```c#\nvar model = new Model() { Value = \"Test\" };\nvar title = \"Test\";\nstring html = new ComponentRenderer\u003cMyComponent\u003e()\n            .Set(c =\u003e c.Model, model)\n            .Set(c =\u003e c.Title, title)\n            .Render();\n```\nMyComponent has a `Model` parameter and a `Title` parameter. The fluent interface uses a lambda expression to specify the property and ensures the value matches the property type.\n\n\u003cdetails\u003e\n  \u003csummary\u003e\u003ccode\u003eMyComponent.razor\u003c/code\u003e\u003c/summary\u003e\n            \n```html\n\u003ch1\u003e@Title\u003c/h1\u003e\n\u003cp\u003eYour model value is @Model.Value\u003c/p\u003e\n@code\n{\n [Parameter] public Model Model { get; set; }\n [Parameter] public string Title { get; set; }\n}\n```\n\n\u003c/details\u003e\n\n**Dependency Injection**\n\nYou can specify services to be provided to a component that uses `@inject`, e.g.:\n```c#\nstring html = new ComponentRenderer\u003cMyComponent\u003e()\n            .AddService\u003cITestService\u003e(new TestService())\n            .Render();\n```\n\n\u003cdetails\u003e\n  \u003csummary\u003e\u003ccode\u003eMyComponent.razor\u003c/code\u003e\u003c/summary\u003e\n            \n```html\n@inject ITestService MyService\n\u003cp\u003eUse service: @MyService.SomeFunction()\u003c/p\u003e\n```\n\n**Add Service Provider**\n\nNew for version 1.5 is the ability to add your own `IServiceProvider` rather than adding services individually. This is useful if you need to re-use the same services repeatedly. Many thanks to [@PhotoAtomic](https://github.com/PhotoAtomic) for this feature!\n```c#\nIServiceProvider myServiceProvider = GetServiceProvider();\nstring html = new ComponentRenderer\u003cMyComponent\u003e()\n            .AddServiceProvider(myServiceProvider)\n            .Render();\n```\n\n\n \n\u003c/details\u003e\n\n**Layouts**\n\nIf a top-level component has a `@layout` attribute it will be applied. Alternatively you can apply a template explicitly:\n```c#\nstring html = new ComponentRenderer\u003cMyComponent\u003e()\n            .UseLayout\u003cMyLayout\u003e()\n            .Render();\n```\nYou can also specify via a type:\n```c#\nvoid Example(Type layout)\n{\n    string html = new ComponentRenderer\u003cMyComponent\u003e()\n            .UseLayout(layout)\n            .Render();\n}\n```\nSee [Layouts](Docs/Layouts.md) for more information\n\n#### The 'kitchen sink'\nYou can chain them all together in any order, provided `.Render()` is last:\n```c#\nvar model = new Model() { Value = \"Test\" };\nvar title = \"Test\";\nstring html = new ComponentRenderer\u003cMyComponent\u003e()\n            .Set(c =\u003e c.Title, title)\n            .AddService\u003cITestService\u003e(new TestService())\n            .Set(c =\u003e c.Model, model)\n            .UseLayout\u003cMyLayout\u003e()\n            .Render();\n```\n\n\u003cdetails\u003e\n  \u003csummary\u003e\u003ccode\u003eMyComponent.razor\u003c/code\u003e\u003c/summary\u003e\n            \n```html\n@inject ITestService MyService\n\u003ch1\u003e@Title\u003c/h1\u003e\n\u003cp\u003eYour model value is @Model.Value\u003c/p\u003e\n\u003cp\u003eUse service: @MyService.SomeFunction()\u003c/p\u003e\n@code\n{\n [Parameter] public Model Model { get; set; }\n [Parameter] public string Title { get; set; }\n}\n```\n\u003c/details\u003e\n\n            \n#### Template Method\nYou can also use the older templater method (retained for compatability). See [Templater](Docs/Templater.md)\n\n## Getting Started\n\nAdd the `BlazorTemplater` NUGET package to your library.\n\n### Usage\n\nSee the [usage guide](Docs/Usage.md).\n\n### Supported Project Types\n\n`BlazorTemplater` can be used in \n - .NET Standard 2.0\n - .NET Standard 2.1\n - .NET Core 3.1\n - .NET 5 \n - .NET 6\n\nLibraries or applications using `BlazorTemplator` need to have the **Razor SDK** enabled to provide compilation and intellisense for `.razor` files. If you have an existing .NET Standard class library that does not have Razor Component support, follow [this guide](Docs/AddRazorSupport.md) to upgrade the library. I did have issues retrofitting Razor support into the .NET Core 3.1 unit test app, so I moved the `.razor` classes into a .NET Standard library `Templater.Library`. This should not be an issue for a Blazor WASM or Blazor Server application using .NET Core 3.1 since they already support.\n\n## Background\n\nHistorically developers have used Razor Views (`.cshtml` files) to create easy-to-edit email templates with model-based data binding.\n\nThis was not a simple process as the `.cshtml` format wasn't always supported in non-web applications or libraries, and getting the syntax-highlighting and tooling to work with Razor Views was sometimes difficult. Two examples of this approach are [RazorLight](https://github.com/toddams/RazorLight) and [RazorEngine](https://github.com/Antaris/RazorEngine).\n\nWith the introduction of [Razor Component Libraries](https://docs.microsoft.com/en-us/aspnet/core/blazor/components/class-libraries) that can contain `.razor` files, it's now much easier to create class libraries which include Razor-based markup than using using Razor Views (`.cshtml` files).\n\n### Technical Differences\n\nA `.cshtml` file was normally a text file or resource in a project that had to be parsed, validated, and a class generated at runtime to create code that could be used with a model to create the HTML required. The meant that RazorView-based libraries had to \"compile\" the template before it could be used, often at runtime.\n\nIn comparison, a **Razor Component** is a class created at _compile time_ so using this is much faster and simpler! Razor Components also support binding of multiple properties (not just a single `Model`), so provides more flexibility in design and usage.\n\n## Supported Features\n\nBlazorRenderer supports using:\n - Rendering of `.razor` templates to HTML\n - Setting `[Parameters]` on Components\n - Injecting [service dependencies](https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection) via `.AddService\u003c..\u003e`\n - Nested Components\n - [Code-behind Components/partial classes](https://docs.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-5.0#partial-class-support)\n - [Cascading Values](https://docs.microsoft.com/en-us/aspnet/core/blazor/components/cascading-values-and-parameters)\n - Layouts\n### Limitations\n\nThe following are not supported/tested:\n   - JavaScript\n   - EventCallbacks\n   - Rerendering\n   - CSS and CSS isolation\n\n#### CSS and Html Emails\n\nCSS support in HTML emails is a complicated area. Many email clients (Outlook, GMail, Hotmail etc) have differing levels of what is supported. You can't often reference an external CSS file from the email as external references are often blocked.\n\nA good idea is to use a utility library to pre-process and inline the CSS before creating the email body. A good example of this is [PreMailer.NET](https://github.com/milkshakesoftware/PreMailer.Net).\n\n## Credits and Acknowledgements\n\nThe basis of the rendering system is adapted from [Steve Sanderson's](https://github.com/SteveSandersonMS) [BlazorUnitTestingPrototype](https://github.com/SteveSandersonMS/BlazorUnitTestingPrototype) repo which was written as very simple component test library for Blazor.\n\nThis was never developed into a functioning product or library. For unit testing Razor Components I recommend [Egil Hansen's bunit](https://github.com/egil/bUnit).\n\n### Version History\n\n| Version  | Changes |\n| -------- |-----------|\n| v1.0.0   | Inital Release (to Nuget) |\n| v1.1.0   | **Breaking change**: renamed `BlazorTemplater` class to `Templater` [#4](https://github.com/conficient/BlazorTemplater/issues/4) |\n| v1.2.0   | Added multi-targetting for .NET Std/.NET 5 to fix bug [#12](https://github.com/conficient/BlazorTemplater/issues/12) |\n| v1.3.0   | Added `ComponentRenderer\u003cT\u003e` for fluent interface and typed parameter setting |\n| v1.4.0   | Added support for Layouts |\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconficient%2FBlazorTemplater","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fconficient%2FBlazorTemplater","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconficient%2FBlazorTemplater/lists"}