forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartialScriptBlock.cs
More file actions
49 lines (41 loc) · 1.86 KB
/
PartialScriptBlock.cs
File metadata and controls
49 lines (41 loc) · 1.86 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ServiceStack.Text;
namespace ServiceStack.Script
{
/// <summary>
/// Partial Block doesn't emit anything it only creates and saves a partial in the PageResult
///
/// Usages: {{#partial mypartial}} contents {{/partial}}
/// {{#partial mypartial {format:'html'} }} contents {{/partial}}
/// {{#partial mypartial {format:'html', pageArg:1} }} contents {{/partial}}
/// </summary>
public class PartialScriptBlock : ScriptBlock
{
public override string Name => "partial";
public override Task WriteAsync(ScriptScopeContext scope, PageBlockFragment block, CancellationToken token)
{
var literal = block.Argument.ParseVarName(out var name);
if (name.IsNullOrEmpty())
throw new NotSupportedException("'partial' block is missing name of partial");
literal = literal.AdvancePastWhitespace();
var argValue = literal.GetJsExpressionAndEvaluate(scope);
var args = argValue as Dictionary<string, object>;
if (argValue != null && args == null)
throw new NotSupportedException("Any 'partial' argument must be an Object Dictionary");
var format = scope.Context.PageFormats.First().Extension;
if (args != null && args.TryGetValue(ScriptConstants.Format, out var oFormat))
{
format = oFormat.ToString();
args.Remove(ScriptConstants.Format);
}
var nameString = name.ToString();
var partial = new SharpPartialPage(scope.Context, nameString, block.Body, format, args);
scope.PageResult.Partials[nameString] = partial;
return TypeConstants.EmptyTask;
}
}
}