forked from toddams/RazorLight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snippets.cs
101 lines (83 loc) · 2.4 KB
/
Snippets.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Threading.Tasks;
using Xunit;
namespace RazorLight.Tests.Snippets
{
using Core;
public class Snippets
{
public class ViewModel
{
public string Name { get; set; }
}
[Fact]
public async Task Simple()
{
#region Simple
var engine = new RazorLightEngineBuilder()
// required to have a default RazorLightProject type,
// but not required to create a template from string.
.UseEmbeddedResourcesProject(typeof(ViewModel))
.SetOperatingAssembly(typeof(ViewModel).Assembly)
.UseMemoryCachingProvider()
.Build();
string template = "Hello, @Model.Name. Welcome to RazorLight repository";
ViewModel model = new ViewModel {Name = "John Doe"};
string result = await engine.CompileRenderStringAsync("templateKey", template, model);
#endregion
Assert.NotNull(result);
}
async Task RenderCompiledTemplate(RazorLightEngine engine, object model)
{
#region RenderCompiledTemplate
var cacheResult = engine.Handler.Cache.RetrieveTemplate("templateKey");
if(cacheResult.Success)
{
var templatePage = cacheResult.Template.TemplatePageFactory();
string result = await engine.RenderTemplateAsync(templatePage, model);
}
#endregion
}
async Task FileSource()
{
#region FileSource
var engine = new RazorLightEngineBuilder()
.UseFileSystemProject("C:/RootFolder/With/YourTemplates")
.UseMemoryCachingProvider()
.Build();
var model = new {Name = "John Doe"};
string result = await engine.CompileRenderAsync("Subfolder/View.cshtml", model);
#endregion
}
async Task EmbeddedResourceSource()
{
#region EmbeddedResourceSource
var engine = new RazorLightEngineBuilder()
.UseEmbeddedResourcesProject(typeof(SomeService).Assembly)
.UseMemoryCachingProvider()
.Build();
var model = new Model();
string html = await engine.CompileRenderAsync("EmailTemplates.Body", model);
#endregion
}
async Task EmbeddedResourceSourceWithRootNamespace()
{
#region EmbeddedResourceSourceWithRootNamespace
var engine = new RazorLightEngineBuilder()
.UseEmbeddedResourcesProject(typeof(SomeService).Assembly, "Project.Core.EmailTemplates")
.UseMemoryCachingProvider()
.Build();
var model = new Model();
string html = await engine.CompileRenderAsync("Body", model);
#endregion
}
public class Model
{
}
}
}
namespace RazorLight.Tests.Snippets.Core
{
public class SomeService
{
}
}