You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39-9Lines changed: 39 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,15 +9,25 @@
9
9
10
10
Compile Razor templates at build-time without a dependency on ASP.NET.
11
11
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.
13
13
14
14
## Usage
15
15
16
16
This package will generate a template class for every `.cshtml` file in your project.
17
17
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:
19
19
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.
21
31
22
32
## Example
23
33
@@ -26,9 +36,10 @@ The following template, in the `TestTemplate.cshtml` file:
26
36
```Razor
27
37
@inherits RazorBlade.HtmlTemplate
28
38
29
-
Hello, @Name!
39
+
Hello, <i>@Name</i>!
30
40
31
-
@functions {
41
+
@functions
42
+
{
32
43
public string? Name { get; set; }
33
44
}
34
45
```
@@ -55,9 +66,28 @@ var template = new TestTemplate
55
66
varresult=template.Render();
56
67
```
57
68
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
+
varmodel=newGreetingModel { Name="World" };
84
+
vartemplate=newTestTemplate(model);
85
+
varresult=template.Render();
86
+
```
59
87
60
-
As of v0.0.2:
88
+
## Additional Features
61
89
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.
0 commit comments