Skip to content

Commit 71f5ef8

Browse files
committed
modular pages
1 parent 2addc8d commit 71f5ef8

File tree

4 files changed

+98
-5
lines changed

4 files changed

+98
-5
lines changed

CSHtml.Examples/ModularHtml.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,75 @@ namespace CSHtml.Examples
88
{
99
public class ModularHtml
1010
{
11+
/// <summary>
12+
/// Example how to write a chunk of html once and use it in other pages
13+
/// </summary>
14+
public static void WritePages()
15+
{
16+
HtmlBuilder page1 = new HtmlBuilder();
17+
HtmlBuilder page2 = new HtmlBuilder();
18+
19+
using (page1.AddTag(HtmlTag.Html))
20+
{
21+
page1.AddChunk(header());
22+
23+
using (page1.AddTag(HtmlTag.Body))
24+
{
25+
page1.WriteLine("Dynamic main content in this html page 001");
26+
}
27+
28+
page1.AddChunk(footer());
29+
}
30+
31+
using (page2.AddTag(HtmlTag.Html))
32+
{
33+
page2.AddChunk(header());
34+
35+
using (page2.AddTag(HtmlTag.Body))
36+
{
37+
page2.WriteLine("Dynamic main content in this html page 002");
38+
}
39+
40+
page2.AddChunk(footer());
41+
}
42+
43+
Console.WriteLine("Write the first page in the terminal");
44+
Console.Write(page1.ToString());
45+
46+
Console.WriteLine();
47+
48+
Console.WriteLine("Write the second page in the terminal");
49+
Console.Write(page2.ToString());
50+
}
51+
52+
private static HtmlBuilder header()
53+
{
54+
HtmlBuilder head = new HtmlBuilder();
55+
56+
using (head.AddTag(HtmlTag.Head))
57+
{
58+
using (head.AddTag(HtmlTag.Title))
59+
{
60+
head.WriteLine("This is the default title for all pages");
61+
}
62+
}
63+
64+
return head;
65+
}
66+
67+
public static HtmlBuilder footer()
68+
{
69+
HtmlBuilder foot = new HtmlBuilder();
70+
71+
using (foot.AddTag(HtmlTag.Footer))
72+
{
73+
using (foot.AddTag(HtmlTag.P))
74+
{
75+
foot.WriteLine("Author: this is the author of the pages");
76+
}
77+
}
78+
79+
return foot;
80+
}
1181
}
1282
}

CSHtml.Examples/SaveDocument.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using System.IO;
62

73
namespace CSHtml.Examples
84
{
95
public class SaveDocument
106
{
7+
/// <summary>
8+
/// Example of how to save the document
9+
/// </summary>
10+
/// <param name="builder"></param>
11+
/// <param name="path"></param>
12+
public void Save(HtmlBuilder builder, string path)
13+
{
14+
using (StreamWriter sw = new StreamWriter(path))
15+
{
16+
sw.Write(builder.ToString());
17+
}
18+
}
1119
}
1220
}

CSHtml/HtmlBuilder.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ public HtmlTagEntry AddTag(HtmlTag tag, params (HtmlAttribute att, string value)
5656
return htag;
5757
}
5858

59+
public void AddChunk(HtmlBuilder builder)
60+
{
61+
var lines = builder.ToString().Split(builder._writer.NewLine.ToCharArray());
62+
63+
foreach (string l in lines)
64+
{
65+
if (string.IsNullOrEmpty(l))
66+
continue;
67+
68+
this.WriteLine(l);
69+
}
70+
}
71+
5972
public void Write(string line, bool tabs = true)
6073
{
6174
if (tabs)

CSHtml/HtmlTag.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public enum HtmlTag
6666

6767
Form,
6868

69+
Footer,
70+
6971
Frame,
7072

7173
Frameset,

0 commit comments

Comments
 (0)