/// <summary> /// Writes the asynchronous. /// </summary> /// <param name="scope">The scope.</param> /// <param name="block">The block.</param> /// <param name="token">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>Task.</returns> /// <exception cref="System.NotSupportedException">'partial' block is missing name of partial</exception> /// <exception cref="System.NotSupportedException">Any 'partial' argument must be an Object Dictionary</exception> 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); }
/// <summary> /// Write as an asynchronous operation. /// </summary> /// <param name="scope">The scope.</param> /// <param name="block">The block.</param> /// <param name="token">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>A Task representing the asynchronous operation.</returns> public override async Task WriteAsync(ScriptScopeContext scope, PageBlockFragment block, CancellationToken token) { var argValue = await block.Argument.GetJsExpressionAndEvaluateAsync(scope); var args = argValue as Dictionary <string, object> ?? new Dictionary <string, object>(); var format = scope.Context.PageFormats.First().Extension; if (args.TryGetValue(ScriptConstants.Format, out var oFormat)) { format = oFormat.ToString(); args.Remove(ScriptConstants.Format); } var htmlDecode = false; if (args.TryGetValue(nameof(htmlDecode), out var oHtmlDecode) && oHtmlDecode is bool b) { htmlDecode = b; args.Remove(nameof(htmlDecode)); } var context = scope.CreateNewContext(args); var unrenderedBody = new SharpPartialPage(scope.Context, "eval-page", block.Body, format, args); using var ms = MemoryStreamFactory.GetStream(); var captureScope = scope.ScopeWith(outputStream: ms, scopedParams: args); await scope.PageResult.WritePageAsync(unrenderedBody, captureScope, token).ConfigAwait(); // ReSharper disable once MethodHasAsyncOverload var renderedBody = ms.ReadToEnd(); if (htmlDecode) { renderedBody = renderedBody.HtmlDecode(); } var pageResult = new PageResult(context.OneTimePage(renderedBody)) { Args = args, }; await pageResult.WriteToAsync(scope.OutputStream, token).ConfigAwait(); }