public static void AddFileAndView(this RazorFormat razorFormat, ViewPageRef viewPage) { var pathProvider = (InMemoryVirtualPathProvider)razorFormat.VirtualPathProvider; pathProvider.AddFile(viewPage.FilePath, viewPage.Contents); razorFormat.AddPage(viewPage); }
/// <summary> /// Pre-compiles the specified template and caches it using the specified name. /// </summary> /// <param name="viewPageRef"> </param> /// <param name="template">The template to precompile.</param> /// <param name="modelType">The type of model used in the template.</param> /// <param name="name">The cache name for the template.</param> public void Compile(ViewPageRef viewPageRef, string template, Type modelType, string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Pre-compiled templates must have a name", "name"); } if (modelType == null) { modelType = new { } }
protected ViewPageRef AddViewPage(string pageName, string pagePath, string pageContents, string templatePath = null) { var dynamicPage = new ViewPageRef(RazorFormat, pagePath, pageName, pageContents, RazorPageType.ViewPage) { Template = templatePath, Service = RazorFormat.TemplateService }; RazorFormat.AddPage(dynamicPage); RazorFormat.TemplateService.RegisterPage(pagePath, pageName); RazorFormat.TemplateProvider.CompileQueuedPages(); return(dynamicPage); }
public void Can_Render_Static_RazorContentPage_that_populates_variable_and_displayed_on_website_template() { var websiteTemplate = @"<!doctype html> <html lang=""en-us""> <head> <title>Static page</title> </head> <body> <header> @RenderSection(""Header"") </header> <div id='menus'> @RenderSection(""Menu"") </div> <h1>Website Template</h1> <div id=""content"">@RenderBody()</div> </body> </html>".NormalizeNewLines(); var template = @"<h1>Static Markdown Template</h1> @section Menu { @Menu(""Links"") } @section Header { <h3>Static Page Title</h3> } <h3>heading 3</h3> <p>paragraph</p>"; var expectedHtml = @"<!doctype html> <html lang=""en-us""> <head> <title>Static page</title> </head> <body> <header> <h3>Static Page Title</h3> </header> <div id='menus'> <ul> <li><a href='About Us'>About Us</a></li> <li><a href='Blog'>Blog</a></li> <li><a href='Links' class='selected'>Links</a></li> <li><a href='Contact'>Contact</a></li> </ul> </div> <h1>Website Template</h1> <div id=""content""><h1>Static Markdown Template</h1> <h3>heading 3</h3> <p>paragraph</p></div> </body> </html>".NormalizeNewLines(); RazorFormat.TemplateService.TemplateBaseType = typeof(CustomViewBase <>); var websiteTemplatePath = "websiteTemplate.cshtml"; RazorFormat.AddFileAndTemplate(websiteTemplatePath, websiteTemplate); var staticPage = new ViewPageRef(RazorFormat, "pagetpl", "StaticTpl", template, RazorPageType.ContentPage) { Service = RazorFormat.TemplateService, Template = websiteTemplatePath, }; RazorFormat.AddPage(staticPage); RazorFormat.TemplateService.RegisterPage("pagetpl", "StaticTpl"); RazorFormat.TemplateProvider.CompileQueuedPages(); var templateOutput = RazorFormat.RenderStaticPage("pagetpl").NormalizeNewLines(); Console.WriteLine(templateOutput); Assert.That(templateOutput, Is.EqualTo(expectedHtml)); }
/// <summary> /// Pre-compiles the specified template and caches it using the specified name. /// </summary> /// <param name="viewPageRef"> </param> /// <param name="template">The template to precompile.</param> /// <param name="name">The cache name for the template.</param> public void Compile(ViewPageRef viewPageRef, string template, string name) { Compile(viewPageRef, template, null, name); }