forked from toddams/RazorLight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnippets.cs
77 lines (63 loc) · 1.85 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
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestPlatform.TestHost;
using RazorLight;
using Xunit;
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(Program))
.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(Program))
.UseMemoryCachingProvider()
.Build();
var model = new SchoolForAnts();
string result = await engine.CompileRenderAsync<object>("Views.Subfolder.SchoolForAnts", model);
#endregion
}
public class SchoolForAnts
{
}
}