Skip to content

Commit 93e02a8

Browse files
committed
v0.1.0
1 parent fca2bac commit 93e02a8

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

README.md

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,25 @@
99

1010
Compile Razor templates at build-time without a dependency on ASP.NET.
1111

12-
:warning: **This is a work-in-progress.** The API is unstable and could change in the future. More features will be added over time.
12+
This is a work-in-progress. Feedback is welcome.
1313

1414
## Usage
1515

1616
This package will generate a template class for every `.cshtml` file in your project.
1717

18-
You can use a `@functions { ... }` block to add properties to your template (instead of a model), which the IDE will see.
18+
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:
1919

20-
The generated template class will inherit from `RazorBlade.HtmlTemplate` by default, but you can customize this with an `@inherits` directive. Specifying the base class explicitly will give you access to its members in the IDE (IntelliSense).
20+
````Razor
21+
@inherits RazorBlade.HtmlTemplate
22+
````
23+
24+
A version with a model is also available for convenience. The following will add a `Model` property and a constructor with a `TModel` parameter:
25+
26+
```Razor
27+
@inherits RazorBlade.HtmlTemplate<TModel>
28+
```
29+
30+
Generated templates are *not* thread-safe. Always use new instances.
2131

2232
## Example
2333

@@ -26,9 +36,10 @@ The following template, in the `TestTemplate.cshtml` file:
2636
```Razor
2737
@inherits RazorBlade.HtmlTemplate
2838
29-
Hello, @Name!
39+
Hello, <i>@Name</i>!
3040
31-
@functions {
41+
@functions
42+
{
3243
public string? Name { get; set; }
3344
}
3445
```
@@ -55,9 +66,28 @@ var template = new TestTemplate
5566
var result = template.Render();
5667
```
5768

58-
## Features
69+
### With a model
70+
71+
A similar template with a model would be:
72+
73+
```Razor
74+
@using MyApplication.Models
75+
@inherits RazorBlade.HtmlTemplate<GreetingModel>
76+
77+
Hello, <i>@Model.Name</i>!
78+
```
79+
80+
Instantiating the generated class requires a model argument:
81+
82+
```C#
83+
var model = new GreetingModel { Name = "World" };
84+
var template = new TestTemplate(model);
85+
var result = template.Render();
86+
```
5987

60-
As of v0.0.2:
88+
## Additional Features
6189

62-
- Use the `@namespace` directive to override the namespace of the generated class.
63-
- You can compose templates by writing them as values: use the `@(new Footer())` syntax for instance.
90+
- The namespace of generated classes is deduced from the file location. This can be overriden with the `@namespace` directive.
91+
- Templates can be composed by writing them as values: `@(new Footer())` for instance.
92+
- A rudimentary HTML helper is provided mostly for `@Html.Raw`
93+
- You can write custom base classes. Constructors decorated with `[TemplateConstructor]` will be available through the generated class.

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4-
<Version>0.0.2</Version>
4+
<Version>0.1.0</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

0 commit comments

Comments
 (0)