internal static async Task EnterScopeAsync(this LiquidTemplateContext context, object?model, Action <Scope> scopeAction) { if (context is LiquidTemplateContextInternal contextInternal) { if (!contextInternal.IsInitialized) { context.AmbientValues.EnsureCapacity(9); context.AmbientValues.Add("Services", context.Services); var shapeRenderer = context.Services.GetRequiredService <IShapeRenderer>(); context.AmbientValues.Add("ShapeRenderer", shapeRenderer); var shapeFactory = context.Services.GetRequiredService <IShapeFactory>(); context.AmbientValues.Add("ShapeFactory", shapeFactory); var options = context.Services.GetRequiredService <IOptions <LiquidOptions> >().Value; context.AddAsyncFilters(options); foreach (var handler in context.Services.GetServices <ILiquidTemplateEventHandler>()) { await handler.RenderingAsync(context); } context.CultureInfo = CultureInfo.CurrentUICulture; contextInternal.IsInitialized = true; } context.EnterChildScope(); if (model != null) { context.MemberAccessStrategy.Register(model.GetType()); } if (context.GetValue("Model")?.ToObjectValue() == model && model is IShape shape) { if (contextInternal.ShapeRecursions++ > LiquidTemplateContextInternal.MaxShapeRecursions) { throw new InvalidOperationException( $"The '{shape.Metadata.Type}' shape has been called recursively more than {LiquidTemplateContextInternal.MaxShapeRecursions} times."); } } else { contextInternal.ShapeRecursions = 0; } } context.SetValue("Model", model); scopeAction?.Invoke(context.LocalScope); }
public Task RenderAsync(string source, TextWriter writer, TextEncoder encoder, object model = null, IEnumerable <KeyValuePair <string, FluidValue> > properties = null) { if (String.IsNullOrWhiteSpace(source)) { return(Task.CompletedTask); } var result = GetCachedTemplate(source); var context = new LiquidTemplateContext(_serviceProvider, _templateOptions); if (properties != null) { foreach (var property in properties) { context.SetValue(property.Key, property.Value); } } return(result.RenderAsync(writer, encoder, context, model)); }
public async Task <IHtmlContent> RenderHtmlContentAsync(string source, TextEncoder encoder, object model = null, IEnumerable <KeyValuePair <string, FluidValue> > properties = null) { if (String.IsNullOrWhiteSpace(source)) { return(HtmlString.Empty); } var result = GetCachedTemplate(source); var context = new LiquidTemplateContext(_serviceProvider, _templateOptions); if (properties != null) { foreach (var property in properties) { context.SetValue(property.Key, property.Value); } } var htmlContentWriter = new ViewBufferTextWriterContent(); await result.RenderAsync(htmlContentWriter, encoder, context, model); return(htmlContentWriter); }