public async Task Does_use_custom_filter() { var context = new TemplatePagesContext { Args = { ["contextArg"] = "foo" }, }.Init(); context.VirtualFiles.WriteFile("page.html", "<h1>{{ 'hello' | echo }}</h1>"); var result = await new PageResult(context.GetPage("page")) { TemplateFilters = { new MyFilter() } }.RenderToStringAsync(); Assert.That(result, Is.EqualTo("<h1>hello hello</h1>")); context.VirtualFiles.WriteFile("page-greet.html", "<h1>{{ 'contextArg' | greetArg }}</h1>"); result = await new PageResult(context.GetPage("page-greet")) { TemplateFilters = { new MyFilter() } }.RenderToStringAsync(); Assert.That(result, Is.EqualTo("<h1>Hello foo</h1>")); }
public async Task Does_reload_modified_page_contents_in_DebugMode() { var context = new TemplatePagesContext { DebugMode = true, //default }; context.VirtualFiles.WriteFile("page.html", "<h1>Original</h1>"); Assert.That(await new PageResult(context.GetPage("page")).RenderToStringAsync(), Is.EqualTo("<h1>Original</h1>")); await Task.Delay(1); //Memory VFS is too fast! context.VirtualFiles.WriteFile("page.html", "<h1>Updated</h1>"); Assert.That(await new PageResult(context.GetPage("page")).RenderToStringAsync(), Is.EqualTo("<h1>Updated</h1>")); }
public async Task Can_generate_html_template_with_layout_in_memory() { var context = new TemplatePagesContext(); context.VirtualFiles.WriteFile("_layout.html", @" <html> <title>{{ title }}</title> </head> <body> {{ page }} </body>"); context.VirtualFiles.WriteFile("page.html", @"<h1>{{ title }}</h1>"); var page = context.GetPage("page"); var result = new PageResult(page) { Args = { { "title", "The Title" }, } }; var html = await result.RenderToStringAsync(); Assert.That(html, Is.EqualTo(@"<html> <title>The Title</title> </head> <body> <h1>The Title</h1> </body>")); }
public async Task Does_explode_Model_properties_of_anon_object_into_scope() { var context = new TemplatePagesContext(); context.VirtualFiles.WriteFile("page.html", @"Id: {{ Id }}, Name: {{ Name }}"); var result = await new PageResult(context.GetPage("page")) { Model = new { Id = 1, Name = "<foo>" } }.RenderToStringAsync(); Assert.That(result, Is.EqualTo("Id: 1, Name: <foo>")); }
public void Context_Throws_FileNotFoundException_when_page_does_not_exist() { var context = new TemplatePagesContext(); Assert.That(context.Pages.GetPage("not-exists.html"), Is.Null); try { var page = context.GetPage("not-exists.html"); Assert.Fail("Should throw"); } catch (FileNotFoundException e) { e.ToString().Print(); } }
public async Task Does_embed_partials() { var context = new TemplatePagesContext { Args = { ["copyright"] = "Copyright © ServiceStack 2008-2017", ["footer"] = "global-footer" } }.Init(); context.VirtualFiles.WriteFile("_layout.html", @" <html> <head><title>{{ title }}</title></head> <body> {{ 'header' | partial }} <div id='content'>{{ page }}</div> {{ footer | partial }} </body> </html> "); context.VirtualFiles.WriteFile("header.html", "<header>{{ pageTitle | titleCase }}</header>"); context.VirtualFiles.WriteFile("page.html", "<h2>{{ contentTitle }}</h2><section>{{ 'page-content' | partial }}</section>"); context.VirtualFiles.WriteFile("page-content.html", "<p>{{ contentBody | padRight(20,'.') }}</p>"); context.VirtualFiles.WriteFile("global-footer.html", "<footer>{{ copyright | raw }}</footer>"); var result = await new PageResult(context.GetPage("page")) { Args = { ["pageTitle"] = "I'm in your header", ["contentTitle"] = "Content is King!", ["contentBody"] = "About this page", } }.RenderToStringAsync(); Assert.That(result.SanitizeNewLines(), Is.EqualTo(@" <html> <head><title>{{ title }}</title></head> <body> <header>I'm In Your Header</header> <div id='content'><h2>Content is King!</h2><section><p>About this page.....</p></section></div> <footer>Copyright © ServiceStack 2008-2017</footer> </body> </html> ".SanitizeNewLines())); }
public async Task Can_generate_markdown_template_with_html_layout_in_memory() { var context = new TemplatePagesContext { PageFormats = { new MarkdownPageFormat() } }; context.VirtualFiles.WriteFile("_layout.html", @" <html> <title>{{ title }}</title> </head> <body> {{ page }} </body>"); context.VirtualFiles.WriteFile("page.md", @"### {{ title }}"); var page = context.GetPage("page"); var result = new PageResult(page) { Args = { { "title", "The Title" }, }, ContentType = MimeTypes.Html, PageTransformers = { MarkdownPageFormat.TransformToHtml }, }; var html = await result.RenderToStringAsync(); Assert.That(html.SanitizeNewLines(), Is.EqualTo(@"<html> <title>The Title</title> </head> <body> <h3>The Title</h3> </body>".SanitizeNewLines())); }
public async Task Can_generate_markdown_template_with_layout_in_memory() { var context = new TemplatePagesContext { PageFormats = { new MarkdownPageFormat() } }; context.VirtualFiles.WriteFile("_layout.md", @" # {{ title }} Brackets in Layout < & > {{ page }}"); context.VirtualFiles.WriteFile("page.md", @"## {{ title }}"); var page = context.GetPage("page"); var result = new PageResult(page) { Args = { { "title", "The Title" }, }, ContentType = MimeTypes.Html, OutputTransformers = { MarkdownPageFormat.TransformToHtml }, }; var html = await result.RenderToStringAsync(); Assert.That(html.SanitizeNewLines(), Is.EqualTo(@"<h1>The Title</h1> <p>Brackets in Layout < & > </p> <h2>The Title</h2>".SanitizeNewLines())); }