-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateExtension.cs
More file actions
56 lines (53 loc) · 1.81 KB
/
Copy pathDateExtension.cs
File metadata and controls
56 lines (53 loc) · 1.81 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
using System;
using System.Globalization;
using Heddle.Attributes;
using Heddle.Core;
using Heddle.Data;
namespace Heddle.Extensions
{
/// <summary>
/// <para>Date template</para>
/// <para>Optional parameter represents date formatting</para>
/// <para>For Example:</para>
/// <para>
/// <code>
/// <%<date>BirthDate[yyyy-MM-dd]%>
/// </code>
/// </para>
/// </summary>
[ExtensionName("date")]
[DataType(typeof(DateTime))]
[EncodeOutput]
public class DateExtension : 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 parentData = scope.Parent();
var dateFormat = GetInnerResult(parentData);
if (string.IsNullOrEmpty(dateFormat))
dateFormat = "d";
if (!(scope.ModelData is DateTime))
return string.Empty;
return ((DateTime) scope.ModelData).ToString(dateFormat, CultureInfo.InvariantCulture);
}
protected override void RenderDataInternal(in Scope scope)
{
var parentData = scope.Parent();
var dateFormat = GetInnerResult(parentData);
if (string.IsNullOrEmpty(dateFormat))
dateFormat = "d";
if (scope.ModelData is DateTime date)
{
#if NET6_0_OR_GREATER
scope.Renderer.Render(date, dateFormat, CultureInfo.InvariantCulture); // phase 8 D10
#else
scope.Renderer.Render(date.ToString(dateFormat, CultureInfo.InvariantCulture));
#endif
}
}
}
}