Skip to content

Commit b6c9f6e

Browse files
Merge branch 'master' into cancellation-token-overloads
2 parents 5c91d58 + 3a41c59 commit b6c9f6e

23 files changed

+645
-289
lines changed

ExampleAppNET5/ExampleAppNET5.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@
99
<ProjectReference Include="..\RazorEngineCore\RazorEngineCore.csproj" />
1010
</ItemGroup>
1111

12+
<ItemGroup>
13+
<None Update="template1.cshtml">
14+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
15+
</None>
16+
<None Update="template2.cshtml">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
1221
</Project>

ExampleAppNET5/Program.cs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Reflection;
45
using System.Runtime.InteropServices;
56
using RazorEngineCore;
@@ -9,45 +10,45 @@ namespace ExampleAppNET5
910

1011
class Program
1112
{
12-
static string Content = @"
13-
Hello @Model.Name
14-
15-
@foreach(var item in @Model.Items)
16-
{
17-
<div>- @item</div>
18-
}
13+
static void Main(string[] args)
14+
{
15+
IRazorEngine razorEngine = new RazorEngine();
16+
IRazorEngineCompiledTemplate template1 = razorEngine.Compile("Hello <h1>@Model.Name</h1>");
1917

20-
<div data-name=""@Model.Name""></div>
18+
string result = template1.Run(new
19+
{
20+
Name = "<b>Alex</b>"
21+
});
2122

22-
<area>
23-
@{ RecursionTest(3); }
24-
</area>
23+
Console.WriteLine(result);
24+
}
2525

26-
@{
27-
void RecursionTest(int level){
28-
if (level <= 0)
29-
{
30-
return;
31-
}
32-
33-
<div>LEVEL: @level</div>
34-
@{ RecursionTest(level - 1); }
35-
}
36-
}";
3726

38-
static void Main(string[] args)
27+
static void ReadFromFileAndRun()
3928
{
4029
IRazorEngine razorEngine = new RazorEngine();
41-
IRazorEngineCompiledTemplate template = razorEngine.Compile(Content);
30+
string templateText = File.ReadAllText("template2.cshtml");
31+
32+
IRazorEngineCompiledTemplate template2 = razorEngine.Compile(templateText, builder =>
33+
{
34+
builder.IncludeDebuggingInfo();
35+
});
36+
37+
if (!Directory.Exists("Temp"))
38+
{
39+
Directory.CreateDirectory("Temp");
40+
}
41+
42+
template2.EnableDebugging("Temp");
4243

43-
string result = template.Run(new
44+
string result = template2.Run(new
4445
{
45-
Name = "Alexander",
46-
Items = new List<string>()
47-
{
48-
"item 1",
49-
"item 2"
50-
}
46+
Name = "Alexander",
47+
Items = new List<string>()
48+
{
49+
"item 1",
50+
"item 2"
51+
}
5152
});
5253

5354
Console.WriteLine(result);

ExampleAppNET5/template1.cshtml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Hello @Model.Name
2+
3+
@foreach(var item in @Model.Items)
4+
{
5+
<div>- @item</div>
6+
}
7+
8+
<div data-name=""@Model.Name""></div>
9+
10+
<area>
11+
@{ RecursionTest(3); }
12+
</area>
13+
14+
@{
15+
void RecursionTest(int level){
16+
if (level <= 0)
17+
{
18+
return;
19+
}
20+
21+
<div>LEVEL: @level</div>
22+
@{ RecursionTest(level - 1); }
23+
}
24+
}

ExampleAppNET5/template2.cshtml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<div>
2+
<h1>Hello</h1>
3+
4+
@{ Breakpoint(); }
5+
6+
@if (Model != null)
7+
{
8+
<h2>Hello @Model</h2>
9+
}
10+
11+
<h3>111</h3>
12+
13+
@if (Model != null)
14+
{
15+
<h2>Hello @Model</h2>
16+
}
17+
18+
<h3>111</h3>
19+
20+
@if (Model != null)
21+
{
22+
<h2>Hello @Model</h2>
23+
}
24+
</div>

Pack.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dotnet build -c Release
22
dotnet test
33
dotnet pack -c Release -o artifacts RazorEngineCore\RazorEngineCore.csproj -p:symbolPackageFormat=snupkg --include-symbols
4-
dotnet nuget push artifacts\RazorEngineCore.2022.8.1.nupkg --source https://www.nuget.org/api/v2/package -k KEY
4+
dotnet nuget push artifacts\RazorEngineCore.2023.11.1.nupkg --source https://www.nuget.org/api/v2/package -k KEY

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,21 @@ IRazorEngineCompiledTemplate compiledTemplate = razorEngine.Compile(templateText
208208
string result = compiledTemplate.Run(new { name = "Hello" });
209209
```
210210

211+
#### Debugging templates
212+
In the builder options, set GeneratePdbStream to true, and set the TemplateFilename.
213+
```cs
214+
razorEngine.Compile(templateSource, builder =>
215+
{
216+
builder.Options.GeneratePdbStream = true;
217+
builder.Options.TemplateFilename = "TemplateFilename.cshtml"
218+
});
219+
```
220+
Your debugger will popup a window asking you to find the source file, after which you can step through as normal.
221+
222+
To set a breakpoint add this line in a code block in the template.
223+
```cs
224+
System.Diagnostics.Debugger.Break();
225+
```
211226

212227
#### Credits
213228
This package is inspired by [Simon Mourier SO post](https://stackoverflow.com/a/47756437/267736)
@@ -216,6 +231,8 @@ This package is inspired by [Simon Mourier SO post](https://stackoverflow.com/a/
216231
#### Changelog
217232
* 2022.8.1
218233
* Proper namespace handling for nested types and types without namespace #113 (thanks [@Kirmiir](https://github.com/Kirmiir))
234+
* 2022.7.6
235+
* Added the option to genereate pdb alongside the assembly which allows debugging the templates.
219236
* 2022.1.2
220237
* #94 publish as single file fix
221238
* 2022.1.1

RazorEngineCore.Tests/TestCompileAndRun.cs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,5 +814,128 @@ private static List<MetadataReference> GetMetadataReferences()
814814
MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime")).Location)
815815
};
816816
}
817+
818+
819+
820+
[TestMethod]
821+
public void TestCompileAndRun_IncludeDebuggingForTypedMode_DisabledDebugging()
822+
{
823+
string templateText = @"
824+
@inherits RazorEngineCore.RazorEngineTemplateBase<RazorEngineCore.Tests.Models.TestModel>
825+
Hello @Model.Decorator(Model.C)
826+
";
827+
828+
RazorEngine razorEngine = new RazorEngine();
829+
IRazorEngineCompiledTemplate<RazorEngineTemplateBase<TestModel>> template = razorEngine.Compile<RazorEngineTemplateBase<TestModel>>(templateText, builder =>
830+
{
831+
builder.IncludeDebuggingInfo();
832+
});
833+
834+
string actual = template.Run(instance =>
835+
{
836+
instance.Model = new TestModel
837+
{
838+
C = "Alex"
839+
};
840+
});
841+
842+
Assert.AreEqual("Hello -=Alex=-", actual.Trim());
843+
}
844+
845+
[TestMethod]
846+
public void TestCompileAndRun_IncludeDebuggingForTypedAnonymous_DisabledDebugging()
847+
{
848+
RazorEngine razorEngine = new RazorEngine();
849+
IRazorEngineCompiledTemplate template = razorEngine.Compile("<h1>Hello @Model.Name</h1>", builder =>
850+
{
851+
builder.IncludeDebuggingInfo();
852+
});
853+
854+
string actual = template.Run(new
855+
{
856+
Name = "Alex"
857+
});
858+
859+
Assert.AreEqual("<h1>Hello Alex</h1>", actual);
860+
}
861+
862+
863+
864+
865+
[TestMethod]
866+
public void TestCompileAndRun_IncludeDebuggingForTypedMode_EnabledDebugging()
867+
{
868+
string templateText = @"
869+
@inherits RazorEngineCore.RazorEngineTemplateBase<RazorEngineCore.Tests.Models.TestModel>
870+
Hello @Model.Decorator(Model.C)
871+
";
872+
873+
RazorEngine razorEngine = new RazorEngine();
874+
IRazorEngineCompiledTemplate<RazorEngineTemplateBase<TestModel>> template = razorEngine.Compile<RazorEngineTemplateBase<TestModel>>(templateText, builder =>
875+
{
876+
builder.IncludeDebuggingInfo();
877+
});
878+
879+
template.EnableDebugging();
880+
881+
string actual = template.Run(instance =>
882+
{
883+
instance.Model = new TestModel
884+
{
885+
C = "Alex"
886+
};
887+
});
888+
889+
Assert.AreEqual("Hello -=Alex=-", actual.Trim());
890+
}
891+
892+
[TestMethod]
893+
public void TestCompileAndRun_IncludeDebuggingForTypedAnonymous_EnabledDebugging()
894+
{
895+
RazorEngine razorEngine = new RazorEngine();
896+
IRazorEngineCompiledTemplate template = razorEngine.Compile("<h1>Hello @Model.Name</h1>", builder =>
897+
{
898+
builder.IncludeDebuggingInfo();
899+
});
900+
901+
template.EnableDebugging();
902+
903+
string actual = template.Run(new
904+
{
905+
Name = "Alex"
906+
});
907+
908+
Assert.AreEqual("<h1>Hello Alex</h1>", actual);
909+
}
910+
911+
[TestMethod]
912+
public void TestCompileAndRun_Typed_EnabledDebuggingThrowsException()
913+
{
914+
string templateText = @"
915+
@inherits RazorEngineCore.RazorEngineTemplateBase<RazorEngineCore.Tests.Models.TestModel>
916+
Hello @Model.Decorator(Model.C)
917+
";
918+
919+
RazorEngine razorEngine = new RazorEngine();
920+
IRazorEngineCompiledTemplate<RazorEngineTemplateBase<TestModel>> template = razorEngine.Compile<RazorEngineTemplateBase<TestModel>>(templateText);
921+
922+
Assert.ThrowsException<RazorEngineException>(() =>
923+
{
924+
template.EnableDebugging();
925+
});
926+
}
927+
928+
[TestMethod]
929+
public void TestCompileAndRun_Anonymous_EnabledDebuggingThrowsException()
930+
{
931+
RazorEngine razorEngine = new RazorEngine();
932+
IRazorEngineCompiledTemplate template = razorEngine.Compile("<h1>Hello @Model.Name</h1>");
933+
934+
Assert.ThrowsException<RazorEngineException>(() =>
935+
{
936+
template.EnableDebugging();
937+
});
938+
939+
}
817940
}
818941
}

0 commit comments

Comments
 (0)