コード例 #1
0
        public override async ValueTask <Completion> WriteToAsync(TextWriter writer, TextEncoder encoder, TemplateContext context)
        {
            if (!context.AmbientValues.TryGetValue("Services", out var servicesValue))
            {
                throw new ArgumentException("Services missing while invoking 'helper'");
            }

            var services = servicesValue as IServiceProvider;

            var viewContextAccessor = services.GetRequiredService <ViewContextAccessor>();
            var viewContext         = viewContextAccessor.ViewContext;

            var arguments = (FilterArguments)(await _arguments.EvaluateAsync(context)).ToObjectValue();
            var helper    = _helper ?? arguments["helper_name"].Or(arguments.At(0)).ToStringValue();

            var factory = services.GetRequiredService <LiquidTagHelperFactory>();

            // Each tag is a singleton, as views are
            if (_activator == null)
            {
                lock (this)
                {
                    if (_activator == null)
                    {
                        _activator = factory.GetActivator(helper, arguments.Names);
                    }
                }
            }

            if (_activator == LiquidTagHelperActivator.None)
            {
                return(Completion.Normal);
            }

            var tagHelper = factory.CreateTagHelper(_activator, (ViewContext)viewContext,
                                                    arguments, out var contextAttributes, out var outputAttributes);

            var content = "";

            // Build the ChildContent of this tag
            using (var sb = StringBuilderPool.GetInstance())
            {
                using (var output = new StringWriter(sb.Builder))
                {
                    if (Statements != null && Statements.Count > 0)
                    {
                        var completion = Completion.Break;

                        for (var index = 0; index < Statements.Count; index++)
                        {
                            completion = await Statements[index].WriteToAsync(output, encoder, context);

                            if (completion != Completion.Normal)
                            {
                                return(completion);
                            }
                        }
                    }

                    await output.FlushAsync();
                }

                content = sb.Builder.ToString();
            }

            var tagHelperContext = new TagHelperContext(contextAttributes, new Dictionary <object, object>(), Guid.NewGuid().ToString("N"));

            var tagHelperOutput = new TagHelperOutput(helper, outputAttributes, (_, e)
                                                      => Task.FromResult(new DefaultTagHelperContent().AppendHtml(content)));

            tagHelperOutput.Content.AppendHtml(content);

            await tagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);

            tagHelperOutput.WriteTo(writer, HtmlEncoder.Default);

            return(Completion.Normal);
        }
コード例 #2
0
        public override async Task <Completion> WriteToAsync(TextWriter writer, TextEncoder encoder, TemplateContext context)
        {
            if (!context.AmbientValues.TryGetValue("Services", out var servicesValue))
            {
                throw new ArgumentException("Services missing while invoking 'helper'");
            }

            var services = servicesValue as IServiceProvider;

            if (!context.AmbientValues.TryGetValue("ViewContext", out var viewContext))
            {
                throw new ArgumentException("ViewContext missing while invoking 'helper'");
            }

            var arguments = (FilterArguments)(await _arguments.EvaluateAsync(context)).ToObjectValue();
            var helper    = _helper ?? arguments["helper_name"].Or(arguments.At(0)).ToStringValue();

            var factory = services.GetRequiredService <LiquidTagHelperFactory>();

            if (_activator == null)
            {
                lock (this)
                {
                    if (_activator == null)
                    {
                        _activator = factory.GetActivator(helper, arguments.Names);
                    }
                }
            }

            if (_activator == LiquidTagHelperActivator.None)
            {
                return(Completion.Normal);
            }

            var tagHelper = factory.CreateTagHelper(_activator, (ViewContext)viewContext,
                                                    arguments, out var contextAttributes, out var outputAttributes);

            var content = new StringWriter();

            if (Statements?.Any() ?? false)
            {
                Completion completion = Completion.Break;
                for (var index = 0; index < Statements.Count; index++)
                {
                    completion = await Statements[index].WriteToAsync(content, encoder, context);

                    if (completion != Completion.Normal)
                    {
                        return(completion);
                    }
                }
            }

            var tagHelperContext = new TagHelperContext(contextAttributes,
                                                        new Dictionary <object, object>(), Guid.NewGuid().ToString("N"));

            var tagHelperOutput = new TagHelperOutput(helper, outputAttributes, (_, e)
                                                      => Task.FromResult(new DefaultTagHelperContent().AppendHtml(content.ToString())));

            tagHelperOutput.Content.AppendHtml(content.ToString());
            await tagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);

            tagHelperOutput.WriteTo(writer, HtmlEncoder.Default);

            return(Completion.Normal);
        }