public void AttachToPage(Page aspnetPage, PageContentToRender contentToRender)
        {
            Verify.ArgumentNotNull(aspnetPage, "aspnetPage");
            Verify.ArgumentNotNull(contentToRender, "contentToRender");

            aspnetPage.Items.Add(PageRenderingJob_Key, contentToRender);

            Guid templateId = contentToRender.Page.TemplateId;
            var  rendering  = _renderingInfo[templateId];

            if (rendering == null)
            {
                Exception loadingException = _loadingExceptions[templateId];
                if (loadingException != null)
                {
                    throw loadingException;
                }

                Verify.ThrowInvalidOperationException("Failed to get master page by template ID '{0}'. Check for compilation errors".FormatWith(templateId));
            }

            aspnetPage.MasterPageFile = rendering.VirtualPath;
            aspnetPage.PreRender     += (e, args) => PageOnPreRender(aspnetPage, contentToRender.Page);

            var master = aspnetPage.Master as MasterPagePageTemplate;

            TemplateDefinitionHelper.BindPlaceholders(master, contentToRender, rendering.PlaceholderProperties, null);
        }
Ejemplo n.º 2
0
        public XDocument Render(PageContentToRender contentToRender, FunctionContextContainer functionContextContainer)
        {
            Guid templateId    = contentToRender.Page.TemplateId;
            var  renderingInfo = _renderingInfo[templateId];

            if (renderingInfo == null)
            {
                Exception loadingException = _loadingExceptions[templateId];
                if (loadingException != null)
                {
                    throw loadingException;
                }

                Verify.ThrowInvalidOperationException($"Missing template '{templateId}'");
            }

            string output;

            RazorPageTemplate webPage = null;

            try
            {
                webPage = WebPageBase.CreateInstanceFromVirtualPath(renderingInfo.ControlVirtualPath) as RazorPageTemplate;
                Verify.IsNotNull(webPage, "Razor compilation failed or base type does not inherit '{0}'",
                                 typeof(RazorPageTemplate).FullName);

                webPage.Configure();

                using (Profiler.Measure("Evaluating placeholders"))
                {
                    TemplateDefinitionHelper.BindPlaceholders(webPage, contentToRender, renderingInfo.PlaceholderProperties,
                                                              functionContextContainer);
                }

                // Executing razor code
                var httpContext = new HttpContextWrapper(HttpContext.Current);
                var startPage   = StartPage.GetStartPage(webPage, "_PageStart", new[] { "cshtml" });
                var pageContext = new WebPageContext(httpContext, webPage, startPage);
                pageContext.PageData.Add(RazorHelper.PageContext_FunctionContextContainer, functionContextContainer);

                var sb = new StringBuilder();
                using (var writer = new StringWriter(sb))
                {
                    using (Profiler.Measure("Executing Razor page template"))
                    {
                        webPage.ExecutePageHierarchy(pageContext, writer);
                    }
                }

                output = sb.ToString();
            }
            finally
            {
                webPage?.Dispose();
            }

            return(XDocument.Parse(output));
        }
        private void ParseTemplate(string virtualPath,
                                   AspNet.Razor.RazorPageTemplate webPage,
                                   out PageTemplateDescriptor templateDescriptor,
                                   out IDictionary <string, PropertyInfo> placeholderProperties)
        {
            Func <PageTemplateDescriptor> constructor = () => new RazorPageTemplateDescriptor(virtualPath);

            templateDescriptor = TemplateDefinitionHelper.BuildPageTemplateDescriptor(webPage, constructor, out placeholderProperties);
        }
Ejemplo n.º 4
0
        public void TestParse_TestFile01()
        {
            var definition = TemplateDefinitionHelper.Parse("TemplateDefinition\\TestFile01.xml");

            Assert.IsNotNull(definition);

            Assert.AreEqual(2, definition.Placeholders.Length);

            Assert.AreEqual("MODULE1", definition.Placeholders[0].Name);
            Assert.AreEqual("Hello World", definition.Placeholders[0].Description);
            Assert.AreEqual("123,456,788", definition.Placeholders[0].SuggestionList);
            Assert.AreEqual("123", definition.Placeholders[0].DefaultValue);

            Assert.AreEqual("MODULE2", definition.Placeholders[1].Name);
            Assert.AreEqual(null, definition.Placeholders[1].Description);
            Assert.AreEqual(null, definition.Placeholders[1].SuggestionList);
            Assert.AreEqual(null, definition.Placeholders[1].Description);
        }
Ejemplo n.º 5
0
        public void TestSplit()
        {
            var splitted = TemplateDefinitionHelper.SplitEscapedString("1,2,3", ',');

            Assert.AreEqual(3, splitted.Length);
            Assert.AreEqual("1", splitted[0]);
            Assert.AreEqual("2", splitted[1]);
            Assert.AreEqual("3", splitted[2]);


            splitted = TemplateDefinitionHelper.SplitEscapedString("1\\,2\\,3", ',');
            Assert.AreEqual(1, splitted.Length);
            Assert.AreEqual("1,2,3", splitted[0]);

            splitted = TemplateDefinitionHelper.SplitEscapedString("1 , 2,3", ',');
            Assert.AreEqual(3, splitted.Length);
            Assert.AreEqual("1", splitted[0]);
            Assert.AreEqual("2", splitted[1]);
            Assert.AreEqual("3", splitted[2]);
        }
Ejemplo n.º 6
0
        internal static void ParseTemplate(string virtualPath,
                                           string filePath,
                                           MasterPagePageTemplate masterPage,
                                           out MasterPagePageTemplateDescriptor pageTemplateDescriptor,
                                           out MasterPageRenderingInfo renderingInfo)
        {
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);

            string csFile = GetCodebehindFilePath(filePath);

            IDictionary <string, PropertyInfo>      placeholderProperties;
            Func <MasterPagePageTemplateDescriptor> constructor = () => new MasterPagePageTemplateDescriptor(filePath, csFile);

            pageTemplateDescriptor = TemplateDefinitionHelper.BuildPageTemplateDescriptor(masterPage, constructor, out placeholderProperties);

            if (pageTemplateDescriptor.Title == null)
            {
                pageTemplateDescriptor.Title = fileNameWithoutExtension;
            }

            renderingInfo = new MasterPageRenderingInfo(virtualPath, placeholderProperties);
        }
Ejemplo n.º 7
0
        private void RendererPage(object sender, EventArgs e)
        {
            Guid templateId    = _job.Page.TemplateId;
            var  renderingInfo = _renderingInfo[templateId];

            if (renderingInfo == null)
            {
                Exception loadingException = _loadingExceptions[templateId];
                if (loadingException != null)
                {
                    throw loadingException;
                }

                Verify.ThrowInvalidOperationException("Missing template '{0}'".FormatWith(templateId));
            }

            string output;
            FunctionContextContainer functionContextContainer;

            RazorPageTemplate webPage = null;

            try
            {
                webPage = WebPageBase.CreateInstanceFromVirtualPath(renderingInfo.ControlVirtualPath) as AspNet.Razor.RazorPageTemplate;
                Verify.IsNotNull(webPage, "Razor compilation failed or base type does not inherit '{0}'",
                                 typeof(AspNet.Razor.RazorPageTemplate).FullName);

                webPage.Configure();

                functionContextContainer = PageRenderer.GetPageRenderFunctionContextContainer();

                using (Profiler.Measure("Evaluating placeholders"))
                {
                    TemplateDefinitionHelper.BindPlaceholders(webPage, _job, renderingInfo.PlaceholderProperties,
                                                              functionContextContainer);
                }

                // Executing razor code
                var httpContext = new HttpContextWrapper(HttpContext.Current);
                var startPage   = StartPage.GetStartPage(webPage, "_PageStart", new[] { "cshtml" });
                var pageContext = new WebPageContext(httpContext, webPage, startPage);
                pageContext.PageData.Add(RazorHelper.PageContext_FunctionContextContainer, functionContextContainer);

                var sb = new StringBuilder();
                using (var writer = new StringWriter(sb))
                {
                    using (Profiler.Measure("Executing Razor page template"))
                    {
                        webPage.ExecutePageHierarchy(pageContext, writer);
                    }
                }

                output = sb.ToString();
            }
            finally
            {
                if (webPage != null)
                {
                    webPage.Dispose();
                }
            }

            XDocument resultDocument = XDocument.Parse(output);

            var     controlMapper = (IXElementToControlMapper)functionContextContainer.XEmbedableMapper;
            Control control       = PageRenderer.Render(resultDocument, functionContextContainer, controlMapper, _job.Page);

            using (Profiler.Measure("ASP.NET controls: PagePreInit"))
            {
                _aspnetPage.Controls.Add(control);
            }
        }
Ejemplo n.º 8
0
        public override async Task <bool> OnNavigatedToAsync(IProgress <string> progress, StepContext ctx)
        {
            if (!IsTargetDirectoryValid(ctx))
            {
                return(false);
            }

            var templateDefinitionFile = Path.Combine(ctx.TargetDirectory, "TemplateDefinition.xml");

            if (!File.Exists(templateDefinitionFile))
            {
                return(false);
            }

            var templateDefinition = await Task.Run(() => TemplateDefinitionHelper.Parse(templateDefinitionFile));

            if (templateDefinition == null)
            {
                return(false);
            }

            try
            {
                File.Delete(templateDefinitionFile);
            }
            catch (Exception ex)
            {
                theLogger.Error($"Unable to delete template definition file: '{templateDefinitionFile}'.", ex);
            }

            foreach (var placeholderDefinition in templateDefinition.Placeholders)
            {
                var placeholder = new PlaceholderViewModel()
                {
                    Name          = placeholderDefinition.Name,
                    TextToReplace = string.IsNullOrEmpty(placeholderDefinition.TextToReplace)
                        ? placeholderDefinition.Name
                        : placeholderDefinition.TextToReplace,
                    Replacement = placeholderDefinition.DefaultValue,
                    Suggestions = TemplateDefinitionHelper.SplitEscapedString(placeholderDefinition.SuggestionList),
                    Description = placeholderDefinition.Description
                };

                placeholder.PropertyChanged += (s, e) =>
                {
                    foreach (var example in Examples)
                    {
                        example.UpdateText(Placeholders);
                    }
                };

                Placeholders.Add(placeholder);
            }

            foreach (var example in templateDefinition.Examples ?? new Example[0])
            {
                Examples.Add(new ExampleViewModel(example));
            }

            return(true);
        }