Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Jan 7, 2024
1 parent 84859cb commit 71b49db
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
[![GitHub release](https://img.shields.io/github/release/ltrzesniewski/RazorBlade.svg?logo=GitHub)](https://github.com/ltrzesniewski/RazorBlade/releases)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/ltrzesniewski/RazorBlade/blob/master/LICENSE)

*The sharpest part of the razor.*
**Compile Razor templates at build-time without a dependency on ASP.NET.**

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

Expand All @@ -33,6 +35,8 @@ A version with a model is also available for convenience. The following will add
<sup><a href='/src/RazorBlade.IntegrationTest/Examples/EmptyTemplateWithModel.cshtml#L1-L1' title='Snippet source file'>snippet source</a> | <a href='#snippet-EmptyTemplateWithModel.cshtml' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Please 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
Expand Down Expand Up @@ -107,6 +111,40 @@ var result = template.Render();
<sup><a href='/src/RazorBlade.IntegrationTest/Examples/Examples.cs#L27-L33' title='Snippet source file'>snippet source</a> | <a href='#snippet-templatewithmodel.usage' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Since 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.

<!-- snippet: TemplateWithManualModel.cshtml -->
<a id='snippet-TemplateWithManualModel.cshtml'></a>
```cshtml
@using MyApplication
@inherits RazorBlade.HtmlTemplate
Hello, <i>@Model.Name</i>!
@functions
{
public required GreetingModel Model { get; init; }
}
```
<sup><a href='/src/RazorBlade.IntegrationTest/Examples/TemplateWithManualModel.cshtml#L1-L9' title='Snippet source file'>snippet source</a> | <a href='#snippet-TemplateWithManualModel.cshtml' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Instantiating the generated class is done similarly to the previous example:

<!-- snippet: TemplateWithManualModel.Usage -->
<a id='snippet-templatewithmanualmodel.usage'></a>
```cs
var model = new GreetingModel { Name = "World" };
var template = new TemplateWithManualModel { Model = model };
var result = template.Render();
```
<sup><a href='/src/RazorBlade.IntegrationTest/Examples/Examples.cs#L38-L44' title='Snippet source file'>snippet source</a> | <a href='#snippet-templatewithmanualmodel.usage' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Documentation

### Base template classes
Expand Down
4 changes: 2 additions & 2 deletions src/NuGetReadme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# RazorBlade

*The sharpest part of the razor.*
**Compile Razor templates at build-time without a dependency on ASP.NET.**

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.

See the [GitHub repository](https://github.com/ltrzesniewski/RazorBlade) for more information.
11 changes: 11 additions & 0 deletions src/RazorBlade.IntegrationTest/Examples/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ public static void TemplateWithModelUsage()

#endregion
}

public static void TemplateWithManualModelUsage()
{
#region TemplateWithManualModel.Usage

var model = new GreetingModel { Name = "World" };
var template = new TemplateWithManualModel { Model = model };
var result = template.Render();

#endregion
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@using MyApplication
@inherits RazorBlade.HtmlTemplate

Hello, <i>@Model.Name</i>!

@functions
{
public required GreetingModel Model { get; init; }
}

0 comments on commit 71b49db

Please sign in to comment.