Пример #1
0
        public override async Task <OdfDocument> RenderAsync(TemplateContext context)
        {
            var outputDocument = new OdfDocument();

            this.TemplateDocument.SaveAs(outputDocument);

            var mainContentTemplate = this.TemplateDocument.ReadTextEntry(this.TemplateDocument.MainContentEntryPath);

            var fluidContext = this.CreateFluidTemplateContext(outputDocument, context);

            using (var ws = outputDocument.OpenOrCreateEntryToWrite(outputDocument.MainContentEntryPath))
                using (var writer = new StreamWriter(ws))
                {
                    await _fluidTemplate.RenderAsync(writer, HtmlEncoder.Default, fluidContext);
                }

            outputDocument.Flush();
            return(outputDocument);
        }
Пример #2
0
        public async Task <string> RenderAsync(string path, object model, ViewDataDictionary viewData, ModelStateDictionary modelState)
        {
            // Check for a custom file provider
            var fileProvider = _options.FileProvider ?? _hostingEnvironment.ContentRootFileProvider;

            var statements = ParseLiquidFile(path, fileProvider, true);

            var template = new FluidTemplate(statements);

            var context = new TemplateContext();

            context.LocalScope.SetValue("Model", model);
            context.LocalScope.SetValue("ViewData", viewData);
            context.LocalScope.SetValue("ModelState", modelState);

            // Provide some services to all statements
            context.AmbientValues.Add("FileProvider", fileProvider);
            context.AmbientValues[ViewPath] = path;
            context.AmbientValues.Add("Sections", new Dictionary <string, IList <Statement> >());
            context.FileProvider = new FileProviderMapper(fileProvider, "Views");

            var body = await template.RenderAsync(context);

            // If a layout is specified while rendering a view, execute it
            if (context.AmbientValues.TryGetValue("Layout", out var layoutPath))
            {
                context.AmbientValues[ViewPath] = layoutPath;
                context.AmbientValues.Add("Body", body);
                var layoutStatements = ParseLiquidFile((string)layoutPath, fileProvider, false);

                var layoutTemplate = new FluidTemplate(layoutStatements);

                return(await layoutTemplate.RenderAsync(context));
            }

            return(body);
        }