forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateService.ServiceStack.cs
More file actions
129 lines (105 loc) · 3.86 KB
/
Copy pathTemplateService.ServiceStack.cs
File metadata and controls
129 lines (105 loc) · 3.86 KB
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
using System;
using System.Collections.Generic;
using System.Dynamic;
using ServiceStack.Common;
using ServiceStack.Html;
using ServiceStack.ServiceHost;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints.Extensions;
namespace ServiceStack.Razor.Templating
{
public partial class TemplateService
{
/// <summary>
/// Runs and returns the template with the specified name.
/// </summary>
public IRazorTemplate ExecuteTemplate<T>(T model, string name, string defaultTemplatePath=null,
IHttpRequest httpReq = null, IHttpResponse httpRes = null)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("The named of the cached template is required.");
ITemplate instance = GetTemplate(name);
if (instance == null)
throw new ArgumentException("No compiled template exists with the specified name.");
SetService(instance, this);
SetModel(instance, model);
TemplateBase.ViewBag = new ExpandoObject();
var razorTemplate = (IRazorTemplate)instance;
razorTemplate.Init(viewEngine, new ViewDataDictionary<T>(model), httpReq, httpRes);
instance.Execute();
var template = httpReq.GetTemplate();
if (template != null)
template = viewEngine.HasTemplate(template) ? template : null;
if (template == null && !razorTemplate.Layout.IsNullOrEmpty())
template = razorTemplate.Layout.MapServerPath();
if (template == null)
template = defaultTemplatePath;
var layoutTemplate = GetTemplate(template ?? RazorFormat.DefaultTemplate);
if (layoutTemplate != null)
{
layoutTemplate.ChildTemplate = razorTemplate;
SetService(layoutTemplate, this);
SetModel(layoutTemplate, model);
layoutTemplate.Execute();
return layoutTemplate;
}
else if (defaultTemplatePath != null)
{
throw new ArgumentException(
"No template exists with the specified Layout: " + defaultTemplatePath);
}
return razorTemplate;
}
readonly Dictionary<string, string> pagePathAndNames = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
public void RegisterPage(string pagePath, string pageName)
{
pagePathAndNames[pagePath] = pageName;
}
public bool ContainsPagePath(string pagePath)
{
return pagePathAndNames.ContainsKey(pagePath);
}
public bool ContainsPageName(string pageName)
{
return pagePathAndNames.ContainsValue(pageName);
}
public IRazorTemplate GetTemplate(string name)
{
ITemplate instance;
if (!templateCache.TryGetValue(name, out instance))
{
var view = viewEngine.GetView(name);
if (view == null)
{
if (name == RazorFormat.DefaultTemplate)
return null;
throw new Exception("Could not find template " + name);
}
view.Compile(); //compiling adds to templateCache
templateCache.TryGetValue(name, out instance);
}
return instance as IRazorTemplate;
}
public IRazorTemplate RenderPartial<T>(T model, string name)
{
var template = GetTemplate(name);
SetService(template, this);
SetModel(template, model);
//TODO: make less ugly,
//since executing templates clears the buffer we need to capture
//what's been rendered and prepend after.
var capture = template.Result;
try
{
template.Execute();
}
catch (Exception ex)
{
throw new InvalidOperationException(
"Could not execute partial: " + name + ", model: " + model);
}
template.Prepend(capture);
return template;
}
}
}