Skip to content

Commit

Permalink
example app update with debugging example
Browse files Browse the repository at this point in the history
  • Loading branch information
adoconnection committed Nov 17, 2023
1 parent caf4ce1 commit 458e6d1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 31 deletions.
9 changes: 9 additions & 0 deletions ExampleAppNET5/ExampleAppNET5.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@
<ProjectReference Include="..\RazorEngineCore\RazorEngineCore.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="template1.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="template2.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
63 changes: 32 additions & 31 deletions ExampleAppNET5/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using RazorEngineCore;
Expand All @@ -9,45 +10,45 @@ namespace ExampleAppNET5

class Program
{
static string Content = @"
Hello @Model.Name
@foreach(var item in @Model.Items)
{
<div>- @item</div>
}
static void Main(string[] args)
{
IRazorEngine razorEngine = new RazorEngine();
IRazorEngineCompiledTemplate template1 = razorEngine.Compile("Hello <h1>@Model.Name</h1>");

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

<area>
@{ RecursionTest(3); }
</area>
Console.WriteLine(result);
}

@{
void RecursionTest(int level){
if (level <= 0)
{
return;
}
<div>LEVEL: @level</div>
@{ RecursionTest(level - 1); }
}
}";

static void Main(string[] args)
static void ReadFromFileAndRun()
{
IRazorEngine razorEngine = new RazorEngine();
IRazorEngineCompiledTemplate template = razorEngine.Compile(Content);
string templateText = File.ReadAllText("template2.cshtml");

IRazorEngineCompiledTemplate template2 = razorEngine.Compile(templateText, builder =>
{
builder.IncludeDebuggingInfo();
});

if (!Directory.Exists("Temp"))
{
Directory.CreateDirectory("Temp");
}

template2.EnableDebugging("Temp");

string result = template.Run(new
string result = template2.Run(new
{
Name = "Alexander",
Items = new List<string>()
{
"item 1",
"item 2"
}
Name = "Alexander",
Items = new List<string>()
{
"item 1",
"item 2"
}
});

Console.WriteLine(result);
Expand Down
24 changes: 24 additions & 0 deletions ExampleAppNET5/template1.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Hello @Model.Name

@foreach(var item in @Model.Items)
{
<div>- @item</div>
}

<div data-name=""@Model.Name""></div>

<area>
@{ RecursionTest(3); }
</area>

@{
void RecursionTest(int level){
if (level <= 0)
{
return;
}

<div>LEVEL: @level</div>
@{ RecursionTest(level - 1); }
}
}
24 changes: 24 additions & 0 deletions ExampleAppNET5/template2.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div>
<h1>Hello</h1>

@{ Breakpoint(); }

@if (Model != null)
{
<h2>Hello @Model</h2>
}

<h3>111</h3>

@if (Model != null)
{
<h2>Hello @Model</h2>
}

<h3>111</h3>

@if (Model != null)
{
<h2>Hello @Model</h2>
}
</div>

0 comments on commit 458e6d1

Please sign in to comment.