This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
NancySparkView.cs
90 lines (72 loc) · 2.35 KB
/
NancySparkView.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
namespace Nancy.ViewEngines.Spark
{
using System.IO;
using System.Web;
using global::Spark;
public abstract class NancySparkView : SparkViewBase
{
private readonly NancyViewData viewData;
protected NancySparkView()
{
this.viewData = new NancyViewData(this);
}
public object Model { get; set; }
public TextWriter Writer { get; set; }
public IRenderContext RenderContext { get; set; }
public void Execute()
{
base.RenderView(Writer);
}
public string H(object value)
{
return HttpUtility.HtmlEncode(value);
}
public object HTML(object value)
{
return value;
}
/// <summary>
/// Non-model specific data for rendering in the response
/// </summary>
public dynamic ViewBag
{
get
{
if (this.RenderContext == null)
{
return null;
}
return this.RenderContext.Context == null ? null : this.RenderContext.Context.ViewBag;
}
}
public string AntiForgeryToken()
{
var tokenKeyValue = this.RenderContext.GetCsrfToken();
return string.Format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\"/>", tokenKeyValue.Key, tokenKeyValue.Value);
}
public virtual void SetModel(object model)
{
this.Model = model;
}
public string SiteResource(string path)
{
return this.RenderContext.ParsePath(path);
}
/// <summary>
/// Non-model specific data retrieved using the <viewdata /> tag in Spark views
/// </summary>
/// <remarks>See more on http://sparkviewengine.com/documentation/variables#Usingviewdata</remarks>
public NancyViewData ViewData
{
get { return this.viewData; }
}
}
public abstract class NancySparkView<TModel> : NancySparkView
{
public new TModel Model { get; private set; }
public override void SetModel(object model)
{
Model = (model is TModel) ? (TModel)model : default(TModel);
}
}
}