Esempio n. 1
0
        public static async ValueTask <string> RenderAsync(this IFluidTemplate template, TemplateContext context, TextEncoder encoder)
        {
            if (context == null)
            {
                ExceptionHelper.ThrowArgumentNullException(nameof(context));
            }

            if (template == null)
            {
                ExceptionHelper.ThrowArgumentNullException(nameof(template));
            }

            var sb     = StringBuilderPool.GetInstance();
            var writer = new StringWriter(sb.Builder);

            // A template is evaluated in a child scope such that the provided TemplateContext is immutable
            context.EnterChildScope();

            try
            {
                await template.RenderAsync(writer, encoder, context);

                writer.Flush();
                return(sb.ToString());
            }
            finally
            {
                sb.Dispose();
                writer.Dispose();
                context.ReleaseScope();
            }
        }
Esempio n. 2
0
        public static void Render(this IFluidTemplate template, TemplateContext context, TextEncoder encoder, TextWriter writer)
        {
            var task = template.RenderAsync(writer, encoder, context);

            if (!task.IsCompletedSuccessfully)
            {
                task.AsTask().GetAwaiter().GetResult();
            }
        }
Esempio n. 3
0
        public static async Task <string> RenderAsync(this IFluidTemplate template, TemplateContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            using (var writer = new StringWriter())
            {
                await template.RenderAsync(writer, HtmlEncoder.Default, context);

                return(writer.ToString());
            }
        }
        public static async Task <string> RenderAsync(this IFluidTemplate template, TextEncoder encoder, TemplateContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }

            using (var sb = StringBuilderPool.GetInstance())
            {
                using (var writer = new StringWriter(sb.Builder))
                {
                    await template.RenderAsync(writer, encoder, context);

                    return(writer.ToString());
                }
            }
        }
Esempio n. 5
0
        public override async ValueTask <Completion> WriteToAsync(TextWriter writer, TextEncoder encoder, TemplateContext context)
        {
            context.IncrementSteps();

            if (_template == null)
            {
                var relativePath = (await Path.EvaluateAsync(context)).ToStringValue();
                if (!relativePath.EndsWith(ViewExtension, StringComparison.OrdinalIgnoreCase))
                {
                    relativePath += ViewExtension;
                }

                var fileProvider = context.Options.FileProvider;

                var fileInfo = fileProvider.GetFileInfo(relativePath);

                if (fileInfo == null || !fileInfo.Exists)
                {
                    throw new FileNotFoundException(relativePath);
                }

                var content = "";

                using (var stream = fileInfo.CreateReadStream())
                    using (var streamReader = new StreamReader(stream))
                    {
                        content = await streamReader.ReadToEndAsync();
                    }

                if (!_parser.TryParse(content, out _template, out var errors))
                {
                    throw new ParseException(errors);
                }

                _identifier = System.IO.Path.GetFileNameWithoutExtension(relativePath);
            }

            try
            {
                context.EnterChildScope();

                if (With != null)
                {
                    var with = await With.EvaluateAsync(context);

                    context.SetValue(_identifier, with);
                }

                if (AssignStatements != null)
                {
                    foreach (var assignStatement in AssignStatements)
                    {
                        await assignStatement.WriteToAsync(writer, encoder, context);
                    }
                }

                await _template.RenderAsync(writer, encoder, context);
            }
            finally
            {
                context.ReleaseScope();
            }

            return(Completion.Normal);
        }
 public static void Render(this IFluidTemplate template, TextWriter writer, TextEncoder encoder, TemplateContext context)
 {
     template.RenderAsync(writer, encoder, context).GetAwaiter().GetResult();
 }
 public static string Render(this IFluidTemplate template)
 {
     return(template.RenderAsync().GetAwaiter().GetResult());
 }
 public static Task <string> RenderAsync(this IFluidTemplate template)
 {
     return(template.RenderAsync(new TemplateContext()));
 }
 public static string Render(this IFluidTemplate template, TemplateContext context)
 {
     return(template.RenderAsync(context).GetAwaiter().GetResult());
 }
Esempio n. 10
0
 public static Task <string> RenderAsync(this IFluidTemplate template, TemplateContext context)
 {
     return(template.RenderAsync(HtmlEncoder.Default, context));
 }
 public static ValueTask <string> RenderAsync(this IFluidTemplate template, TemplateContext context)
 {
     return(template.RenderAsync(context, NullEncoder.Default));
 }
Esempio n. 12
0
        public static string Render(this IFluidTemplate template)
        {
            var task = template.RenderAsync();

            return(task.IsCompletedSuccessfully ? task.Result : task.AsTask().GetAwaiter().GetResult());
        }
Esempio n. 13
0
        public static string Render(this IFluidTemplate template, TemplateContext context, TextEncoder encoder)
        {
            var task = template.RenderAsync(context, encoder);

            return(task.IsCompletedSuccessfully ? task.Result : task.AsTask().GetAwaiter().GetResult());
        }