Skip to content

Commit 46a7ad5

Browse files
committed
Created demo for code behind
1 parent 6f3ad89 commit 46a7ad5

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

_home.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
@hlp.TutorialLinkHome("Using Emojis / Emoticons 👍", "html210", "Show Emojis in your output or use them for showing true/false", "")
2525

26-
@hlp.TutorialLinkHome("Reuse templates and code", "reuse", "You can place common templates, hlp and functions into shared files - both <code>.cshtml</code> and <code>.cs</code>.", "")
26+
@hlp.TutorialLinkHome("Reuse templates and code", "reuse", "You can place common templates, hlp and functions into shared files - both <code>.cshtml</code> and <code>.cs</code>. And you can split Razor files into code and templates.", "enhanced for v11")
2727
</ol>
2828

2929
<hr>

reuse/_000-reuse.cshtml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@
2020
@helpers.TutorialLink("Reuse code with .cs files",
2121
"reuse320",
2222
"CreateInstance is enhanced in 2sxc 10.01 to support .cs files to share code across Razor files and WebApi Controllers.",
23-
"new in 10.01")
23+
"new in v10")
24+
@helpers.TutorialLink("Split Razor Templates into C#-Code and Template",
25+
"reuse410",
26+
"Now you can split out complicated C# code so designers don't have to worry about it. ",
27+
"new in v11")
2428
</ol>

reuse/_410-code-behind.code.cshtml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@* Code-Behind files must inherit from RazorComponentCode *@
2+
@inherits ToSic.Sxc.Dnn.RazorComponentCode
3+
4+
@* Functions can just be called by the parent like normal C# functions*@
5+
@functions {
6+
public string Hello() {
7+
return "Hello from inner code";
8+
}
9+
}
10+
11+
@* Helpers can be used in the razor code like @Message *@
12+
@helper Message(string message) {
13+
<div class="alert alert-success" >@message</div>
14+
}

reuse/_410-code-behind.cshtml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
@* Note that the Code-Behind features requires this @inherits statement *@
2+
@inherits ToSic.Sxc.Dnn.RazorComponent
3+
<hide>
4+
@RenderPage("_header.cshtml")
5+
</hide>
6+
7+
<trim>
8+
<h2>Use Code-Behind in Razor Templates (new in V11)</h2>
9+
<p>
10+
Razor Templates should be simple to edit for web designers. When you end up getting complex functions we recommend placing them in a Code-Behind file. This is different from shared code, since the code isn't shared. We're just splitting a razor file into the code-parts and design parts.
11+
</p>
12+
</trim>
13+
14+
<hr>
15+
<h2>Calling a function from the code-behind</h2>
16+
<trim>
17+
<p>This calls the function <code>Hello()</code> from the Code-Behind.</p>
18+
</trim>
19+
20+
<div>Hello from Code Behind: <strong>@Code.Hello()</strong></div>
21+
22+
<hr>
23+
<h2>Calling a helper from the code-behind</h2>
24+
<trim>
25+
<p>This creates a special alert-div using a Helper called <code>Message</code> from the Code-Behind.</p>
26+
</trim>
27+
28+
@Code.Message("Message in a special format")
29+
30+
<hide>
31+
@RenderPage("../shared/_source-code.cshtml", new { Path = VirtualPath })
32+
33+
@RenderPage("../shared/_source-code.cshtml", new {
34+
Path = VirtualPath,
35+
File = "_410-code-behind.code.cshtml",
36+
Size = 400,
37+
// Language = "csharp"
38+
})
39+
</hide>

0 commit comments

Comments
 (0)