-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtension.cs
More file actions
74 lines (66 loc) · 2.06 KB
/
Copy pathStringExtension.cs
File metadata and controls
74 lines (66 loc) · 2.06 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
using System;
using System.Globalization;
using Heddle.Attributes;
using Heddle.Core;
using Heddle.Data;
namespace Heddle.Extensions
{
/// <summary>
/// <para>String Template</para>
/// <para>Optional parameter represents default value if data name is not set in template or the object value is null</para>
/// </summary>
[ExtensionName("string")]
[DataType(typeof(string))]
[EncodeOutput]
public class StringExtension : AbstractHtmlExtension
{
public override ExType InitStart(InitContext initContext, ExType dataType, ExType chainedType, ExType parent)
{
return base.InitStart(initContext, parent, chainedType, null);
}
protected override object ProcessDataInternal(in Scope scope)
{
var model = scope.ModelData;
if (model == null)
{
var parentScope = scope.Parent();
return GetInnerResult(parentScope);
}
if (model is string)
return model;
try
{
model = Convert.ChangeType(model, typeof(string), CultureInfo.InvariantCulture);
}
catch (InvalidCastException)
{
return model.ToString();
}
return model;
}
protected override void RenderDataInternal(in Scope scope)
{
var model = scope.ModelData;
if (model == null)
{
var parentScope = scope.Parent();
RenderInnerResult(parentScope);
return;
}
if (model is string data)
{
scope.Renderer.Render(data);
return;
}
try
{
var str = (string) Convert.ChangeType(model, typeof(string), CultureInfo.InvariantCulture);
scope.Renderer.Render(str);
}
catch (InvalidCastException)
{
scope.Renderer.Render(model.ToString());
}
}
}
}