https://github.com/ltrzesniewski/RazorBlade
Compile Razor templates at build-time without a dependency on ASP.NET.
https://github.com/ltrzesniewski/RazorBlade
razor template
Last synced: 20 days ago
JSON representation
Compile Razor templates at build-time without a dependency on ASP.NET.
- Host: GitHub
- URL: https://github.com/ltrzesniewski/RazorBlade
- Owner: ltrzesniewski
- License: mit
- Created: 2022-09-05T19:28:10.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-05T19:53:39.000Z (about 1 month ago)
- Last Synced: 2025-04-12T00:58:20.265Z (26 days ago)
- Topics: razor, template
- Language: C#
- Homepage:
- Size: 355 KB
- Stars: 159
- Watchers: 5
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - https://github.com/ltrzesniewski/RazorBlade
README
# RazorBlade
[](https://github.com/ltrzesniewski/RazorBlade/actions?query=workflow%3ABuild)
[](https://www.nuget.org/packages/RazorBlade)
[](https://github.com/ltrzesniewski/RazorBlade/releases)
[](https://github.com/ltrzesniewski/RazorBlade/blob/master/LICENSE)**Compile Razor templates at build-time without a dependency on ASP.NET.**
RazorBlade is meant to be *lightweight* and *self-contained*: cshtml files are compiled into C# classes at build-time with a Roslyn source generator. No reference to ASP.NET is required.
A simple base class library is provided by default, but it can also be embedded into the target project, or even replaced by your own implementation.
## Usage
This package will generate a template class for every `.cshtml` file in your project.
The generated classes will inherit from `RazorBlade.HtmlTemplate` by default, though it is advised to specify the base class explicitly to get the best IDE experience:
```cshtml
@inherits RazorBlade.HtmlTemplate
```
snippet source | anchorA version with a model is also available for convenience. The following will add a `Model` property and a constructor with a `ModelType` parameter:
```cshtml
@inherits RazorBlade.HtmlTemplate
```
snippet source | anchorPlease note that this will cause a constructor with a `ModelType` parameter to be added to the generated class, which may cause false errors to be shown in some IDEs.
Further [documentation](#Documentation) is provided below.
## Example
The following template, in the `ExampleTemplate.cshtml` file:
```cshtml
@inherits RazorBlade.HtmlTemplateHello, @Name!
@functions
{
public string? Name { get; init; }
}
```
snippet source | anchorWill generate the following class in your project:
```cs
internal partial class ExampleTemplate : RazorBlade.HtmlTemplate
{
// ...
public string? Name { get; init; }
// ...
}
```That you can use like the following:
```cs
var template = new ExampleTemplate
{
Name = "World"
};var result = template.Render();
```
snippet source | anchor### With a model
A similar template with a model would be:
```cshtml
@using MyApplication
@inherits RazorBlade.HtmlTemplateHello, @Model.Name!
```
snippet source | anchorInstantiating the generated class requires a model argument:
```cs
var model = new GreetingModel { Name = "World" };
var template = new TemplateWithModel(model);
var result = template.Render();
```
snippet source | anchorSince this generates a constructor with a `GreetingModel` parameter in the `TemplateWithModel` class, it may cause false errors to be shown in some IDEs, as they don't recognize this constructor signature.
### With a manual model property
Another way of implementing a template with a model is to add a `Model` property in the template and mark it as `required`. This will work around false errors which can be shown in some IDEs.
```cshtml
@using MyApplication
@inherits RazorBlade.HtmlTemplateHello, @Model.Name!
@functions
{
public required GreetingModel Model { get; init; }
}
```
snippet source | anchorInstantiating the generated class is done similarly to the previous example:
```cs
var model = new GreetingModel { Name = "World" };
var template = new TemplateWithManualModel { Model = model };
var result = template.Render();
```
snippet source | anchor## Documentation
### Base template classes
For HTML templates, specify one of the following base classes with an `@inherits` directive:
- `RazorBlade.HtmlTemplate`
- `RazorBlade.HtmlTemplate`
- `RazorBlade.HtmlTemplateWithLayout` (automatically applies the given layout)
- `RazorBlade.HtmlLayout` (for layouts only)If you'd like to write a plain text template (which never escapes HTML), the following classes are available:
- `RazorBlade.PlainTextTemplate`
- `RazorBlade.PlainTextTemplate`They all derive from `RazorBlade.RazorTemplate`, which provides the base functionality.
You can also write your own base classes. Marking a constructor with `[TemplateConstructor]` will forward it to the generated template class.
### Writing templates
HTML escaping can be avoided by using the `@Html.Raw(value)` method, just like in ASP.NET. The `IEncodedContent` interface represents content which does not need to be escaped. The `HtmlString` class is a simple implementation of this interface.
Templates can be included in other templates by evaluating them, since they implement `IEncodedContent`. For instance, a `Footer` template can be included by writing `@(new Footer())`. Remember to always create a new instance of the template to include, even if it doesn't contain custom code, as templates are stateful and not thread-safe.
The namespace of the generated class can be customized with the `@namespace` directive. The default value is deduced from the file location.
### Layouts
Layout templates may be written by inheriting from the `RazorBlade.HtmlLayout` class, which provides the relevant methods such as `RenderBody` and `RenderSection`. It inherits from `RazorBlade.HtmlTemplate`.
The layout to use can be specified by overriding the `CreateLayout` method of `RazorBlade.HtmlTemplate`. Given that all Razor templates are stateful and not thread-safe, always create a new instance of the layout page to use:
```cshtml
@functions
{
protected override HtmlLayout? CreateLayout()
=> new LayoutToUse();
}
```
snippet source | anchorThis can be simplified by using the `HtmlTemplateWithLayout` class, which can be useful in `_ViewImports.cshtml` files:
```cshtml
@inherits RazorBlade.HtmlTemplateWithLayout
```
snippet source | anchorLayout pages can be nested, and can use sections. Unlike in ASP.NET, RazorBlade does not verify if the body and all sections have been used. Sections may also be executed multiple times.
> [!NOTE]
> Layout usage is not compatible with direct output to the provided `TextWriter` and will cause buffering.
> You may work around it by replacing layouts with partial templates such as:
>
> ```cshtml
> @(new Header())
> Your content
> @(new Footer())
> ```### Import files: `_ViewImports.cshtml`
RazorBlade automatically applies the [`_ViewImports.cshtml` files](https://learn.microsoft.com/en-us/aspnet/core/mvc/views/layout#importing-shared-directives) from the project root to the given template directory.
These files can define default values for `@using`, `@inherits` and `@namespace`. For instance, an `@inherits RazorBlade.HtmlTemplateWithLayout` directive in a `_ViewImports.cshtml` file will set a common default layout type for the templates in its directory and subdirectories.
### Executing templates
The `RazorTemplate` base class provides `Render` and `RenderAsync` methods to execute the template.
Templates are stateful and not thread-safe, so it is advised to always create new instances of the templates to render.
### MSBuild
The source generator will process `RazorBlade` MSBuild items which have the `.cshtml` file extension.
By default, all `.cshtml` files are included, unless one of the `EnableDefaultRazorBladeItems` or `EnableDefaultItems` properties are set to `false`. You can also manually customize this set.
**Available property settings:**
- `EnableDefaultRazorBladeItems`: Whether to automatically include all `.cshtml` files in the project. Default is `true`.
- `RazorBladeDefaultAccessibility`: The default accessibility of the generated classes (`internal` or `public`). Default is `internal`.
- `RazorBladeEmbeddedLibrary`: Whether to embed the RazorBlade library in the target project (see below). Default is `false`.**Available item metadata settings:**
- `Accessibility`: The accessibility of the generated class (`internal` or `public`). Default is `$(RazorBladeDefaultAccessibility)`.
### Removing the dependency on RazorBlade
RazorBlade makes it possible to remove the dependency on its runtime assembly. This could be useful for library projects which should be self-contained, with no dependencies on external packages.
This mode is enabled by default when the `PackageReference` of RazorBlade has the `PrivateAssets="all"` attribute. In order to avoid compilation warnings, the assembly reference also needs to be explicitly excluded with `ExcludeAssets="compile;runtime"`.
```XML
```
A source generator will then embed an `internal` version of the RazorBlade library in the target project. This behavior can also be controlled by setting the `RazorBladeEmbeddedLibrary` MSBuild property to `true` or `false`.