示例#1
0
        private static string AddLayoutToPage(ApplicationDbContext context, RenderedTemplate renderedTemplate, List <string> assemblies, Page page)
        {
            IncludeRazorEngine engine      = new IncludeRazorEngine(assemblies);
            RazorEngine        razorEngine = new RazorEngine();

            RazorEngineCompiledTemplate <LayoutTemplateBase> compiledLayout =
                razorEngine.Compile <LayoutTemplateBase>(context.Layouts.Find(renderedTemplate.Layout).Contents,
                                                         engine.GetOptionsBuilder);

            string renderCss = page.CssContents;
            string renderJs  = page.JsContents;

            foreach (var component in renderedTemplate.ComponentDependencies.Distinct().Select(componentDependency =>
                                                                                               context.Components.Find(componentDependency)))
            {
                renderCss += "\n" + component.CssContents;
                renderJs  += "\n" + component.JsContents;
            }

            return(engine.RenderLayout(compiledLayout, new
            {
                RenderBody = renderedTemplate.HtmlContents,
                RenderCss = renderCss,
                // InnerJsContents already has a \n at the beginning of it
                RenderJs = renderJs + renderedTemplate.InnerJsContents,
                ViewData = renderedTemplate.ViewData
            }));
        }
示例#2
0
        private static string RenderPage(ApplicationDbContext context,
                                         Dictionary <string, RazorEngineCompiledTemplate <IncludeTemplateBase> > compiledIncludes, List <string> assemblies, Page page)
        {
            IncludeRazorEngine engine      = new IncludeRazorEngine(assemblies);
            RazorEngine        razorEngine = new RazorEngine();

            RazorEngineCompiledTemplate <IncludeTemplateBase> compiledTemplate =
                razorEngine.Compile <IncludeTemplateBase>(page.HtmlContents, engine.GetOptionsBuilder);

            RenderedTemplate renderedTemplate = engine.Render(compiledTemplate, compiledIncludes, new { });

            // Remove any inline script elements and store them separately
            List <string> inlineScripts = new List <string>();
            XmlDocument   doc           = new XmlDocument();

            // Wrap the page in a PageContainer so that including text outside of any element doesn't throw an error
            doc.AppendChild(doc.CreateElement("PageContainer"));
            doc.GetElementsByTagName("PageContainer")[0].InnerXml = renderedTemplate.HtmlContents;
            while (doc.GetElementsByTagName("script").Count > 0)
            {
                var node = doc.GetElementsByTagName("script")[0];
                inlineScripts.Add(node.InnerText);
                node.ParentNode.RemoveChild(node);
            }

            renderedTemplate.HtmlContents    = doc.GetElementsByTagName("PageContainer")[0].InnerXml;
            renderedTemplate.InnerJsContents =
                inlineScripts.Aggregate(string.Empty, (current, script) => current + "\n" + script);

            return(renderedTemplate.Layout is null ? renderedTemplate.HtmlContents : AddLayoutToPage(context, renderedTemplate, assemblies, page));
        }
示例#3
0
        public RenderedTemplate Render(RazorEngineCompiledTemplate <IncludeTemplateBase> compiledTemplate,
                                       IDictionary <string, RazorEngineCompiledTemplate <IncludeTemplateBase> > compiledIncludes, object model)
        {
            List <string> usedIncludes   = new List <string>();
            string        templateLayout = string.Empty;
            Dictionary <string, dynamic> templateViewData = new Dictionary <string, dynamic>();
            string templateOutput = compiledTemplate.Run(instance =>
            {
                if (!(model is AnonymousTypeWrapper))
                {
                    model = new AnonymousTypeWrapper(model);
                }

                instance.Model           = model;
                instance.IncludeCallback = (key, includeModel) =>
                {
                    usedIncludes.Add(key);
                    RenderedTemplate renderedTemplate = Render(compiledIncludes[key], compiledIncludes, includeModel);
                    renderedTemplate.ViewData.ToList().ForEach(item => { templateViewData[item.Key] = item.Value; });
                    return(renderedTemplate.HtmlContents);
                };
                instance.LayoutCallback = key =>
                {
                    templateLayout = key;
                };
                instance.ViewData.Callback = newViewData =>
                {
                    templateViewData = newViewData;
                };
            });

            return(new RenderedTemplate
            {
                HtmlContents = templateOutput,
                ComponentDependencies = usedIncludes,
                Layout = templateLayout,
                ViewData = templateViewData
            });
        }