-
Notifications
You must be signed in to change notification settings - Fork 1
/
CobaltTemplate.cs
144 lines (107 loc) · 4.38 KB
/
CobaltTemplate.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System.IO;
using System.Web;
using System.Web.Compilation;
using System.Web.UI;
using Cobalt.Html;
using Cobalt.Web;
using Cobalt.Web.Mvc;
using System.Collections.Generic;
namespace Cobalt {
/// <summary>
/// CobaltElement designed for loading external templates
/// </summary>
public class CobaltTemplate : CobaltControl {
#region Constructors
/// <summary>
/// Loads an external template
/// </summary>
public CobaltTemplate(string path) {
this._Path = path;
this._LoadTemplate();
}
#endregion
#region Properties
private string _Path;
#endregion
#region Loading Templates
private Dictionary<string, string> _LoadedTemplates {
get {
Dictionary<string, string> templates = HttpContext.Current.Items["LoadedTemplates:Content"] as Dictionary<string, string>;
if (templates == null) {
templates = new Dictionary<string, string>();
HttpContext.Current.Items.Add("LoadedTemplates:Content", templates);
}
return templates;
}
}
//determines the correct way to load the content
private void _LoadTemplate() {
//determine how to handle this
this._Path = (this._Path ?? string.Empty).Trim().ToLower();
if (this._Path.EndsWith(".aspx")) {
this._LoadPageTemplate();
}
else if (this._Path.EndsWith(".ascx")) {
this._LoadUserControlTemplate();
}
else {
this._LoadContentTemplate();
}
}
//loads an entire page
private void _LoadPageTemplate() {
//get and instance of the page
Control control = BuildManager.CreateInstanceFromVirtualPath(this._Path, typeof(Control)) as Control;
//get a separate rendering context
StringWriter write = new StringWriter();
HttpContext context = new HttpContext(HttpContext.Current.Request, new HttpResponse(write));
context.Handler = control as IHttpHandler;
//process and get the content created
context.Handler.ProcessRequest(context);
string content = write.ToString();
//select the content for the view
this.SelectWithoutConstruct(HtmlNode.Parse(content));
}
//loads an external user control
private void _LoadUserControlTemplate() {
//get a separate rendering context
StringWriter write = new StringWriter();
HttpContext context = new HttpContext(HttpContext.Current.Request, new HttpResponse(write));
//load the control
CobaltTemplateRenderPage page = new CobaltTemplateRenderPage();
Control control = page.LoadControl(this._Path);
control.Page = page;
//try and render this content
using (HtmlTextWriter writer = new HtmlTextWriter(write)) {
control.RenderControl(writer);
}
//render the content (which will end up in a context
//container and should use children instead)
string content = write.ToString();
CobaltElement container = new CobaltElement(content);
//select the content for the view
this.SelectWithoutConstruct(container.Children());
}
//loads generic HTML content
private void _LoadContentTemplate() {
//update the path if needed
string path = this._Path.StartsWith("~")
? HttpContext.Current.Server.MapPath(this._Path)
: this._Path;
//check if this was already loaded
path = path.ToLower();
//check in memory for this template
string content = null;// File.ReadAllText(path);
if (this._LoadedTemplates.ContainsKey(path)) {
content = this._LoadedTemplates[path];
}
else {
content = File.ReadAllText(path);
this._LoadedTemplates.Add(path, content);
}
//select the content to use
this.SelectWithoutConstruct(HtmlNode.Parse(content));
}
#endregion
}
}