-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElifExtension.cs
More file actions
57 lines (49 loc) · 1.92 KB
/
Copy pathElifExtension.cs
File metadata and controls
57 lines (49 loc) · 1.92 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
using Heddle.Attributes;
using Heddle.Core;
using Heddle.Data;
namespace Heddle.Extensions
{
/// <summary>
/// <para>Continues a branch set: renders its body when no earlier branch of the set fired and its own
/// condition is truthy, then publishes the updated <see cref="BranchState"/>. When an earlier branch
/// already fired, it renders nothing and leaves the state unchanged.</para>
/// <para>With no set open it behaves exactly like <c>@if</c> (starts a set) and draws <c>HED3002</c>
/// where statically visible. Stateless — safe for concurrent renders.</para>
/// </summary>
[ExtensionName("elif")]
[ExtensionName("elseif")]
[ScopeChannel]
[BranchRole(BranchRole.Continuation)]
public class ElifExtension : AbstractExtension
{
public override ExType InitStart(InitContext initContext, ExType dataType, ExType chainedType, ExType parent)
{
return base.InitStart(initContext, parent, chainedType, null);
}
public override object ProcessData(in Scope scope)
{
if (scope.TryReadBranch(out var state) && state.Satisfied)
return string.Empty;
bool truthy = BranchCondition.IsTruthy(scope.ModelData);
scope.PublishBranch(new BranchState(truthy));
if (truthy)
{
var parentData = scope.Parent();
return GetInnerResult(parentData);
}
return string.Empty;
}
public override void RenderData(in Scope scope)
{
if (scope.TryReadBranch(out var state) && state.Satisfied)
return;
bool truthy = BranchCondition.IsTruthy(scope.ModelData);
scope.PublishBranch(new BranchState(truthy));
if (truthy)
{
var parentData = scope.Parent();
RenderInnerResult(parentData);
}
}
}
}