forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIfScriptBlock.cs
More file actions
33 lines (30 loc) · 1.11 KB
/
IfScriptBlock.cs
File metadata and controls
33 lines (30 loc) · 1.11 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
using System;
using System.Threading;
using System.Threading.Tasks;
using ServiceStack.Text;
namespace ServiceStack.Script
{
/// <summary>
/// Handlebars.js like if block
/// Usages: {{#if a > b}} max {{a}} {{/if}}
/// {{#if a > b}} max {{a}} {{else}} max {{b}} {{/if}}
/// {{#if a > b}} max {{a}} {{else if b > c}} max {{b}} {{else}} max {{c}} {{/if}}
/// </summary>
public class IfScriptBlock : ScriptBlock
{
public override string Name => "if";
public override async Task WriteAsync(ScriptScopeContext scope, PageBlockFragment block, CancellationToken token)
{
var result = await block.Argument.GetJsExpressionAndEvaluateToBoolAsync(scope,
ifNone: () => throw new NotSupportedException("'if' block does not have a valid expression")).ConfigAwait();
if (result)
{
await WriteBodyAsync(scope, block, token).ConfigAwait();
}
else
{
await WriteElseAsync(scope, block.ElseBlocks, token).ConfigAwait();
}
}
}
}