Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public override async Task WriteAsync(ScriptScopeContext scope, PageBlockFragment block, CancellationToken token)
        {
            var argValue = block.Argument.GetJsExpressionAndEvaluate(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);

                var renderedBody = await ms.ReadToEndAsync();

                if (htmlDecode)
                {
                    renderedBody = renderedBody.HtmlDecode();
                }

                var pageResult = new PageResult(context.OneTimePage(renderedBody))
                {
                    Args = args,
                };
                await pageResult.WriteToAsync(scope.OutputStream, token);
            }
        }