forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithScriptBlock.cs
More file actions
35 lines (30 loc) · 1.17 KB
/
WithScriptBlock.cs
File metadata and controls
35 lines (30 loc) · 1.17 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
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ServiceStack.Script
{
/// <summary>
/// Handlebars.js like with block
/// Usages: {{#with person}} Hi {{name}}, I'm {{age}} years old{{/with}}
/// {{#with person}} Hi {{name}}, I'm {{age}} years old {{else}} no person {{/with}}
/// </summary>
public class WithScriptBlock : ScriptBlock
{
public override string Name => "with";
public override async Task WriteAsync(ScriptScopeContext scope, PageBlockFragment block, CancellationToken token)
{
var result = await block.Argument.GetJsExpressionAndEvaluateAsync(scope,
ifNone: () => throw new NotSupportedException("'with' block does not have a valid expression"));
if (result != null)
{
var resultAsMap = result.ToObjectDictionary();
var withScope = scope.ScopeWithParams(resultAsMap);
await WriteBodyAsync(withScope, block, token);
}
else
{
await WriteElseAsync(scope, block.ElseBlocks, token);
}
}
}
}