Ejemplo n.º 1
0
        public void Can_inherit_from_CustomViewPage_using_inherits_directive()
        {
            var template = @"@inherits ServiceStack.ServiceHost.Tests.Formats_Razor.CustomViewBase<ServiceStack.ServiceHost.Tests.Formats_Razor.Person>
<h1>Generic View Page</h1>

<h2>Form fields</h2>
@Html.LabelFor(m => m.FirstName) @Html.TextBoxFor(m => m.FirstName)

<h2>Person Table</h2>
@Table(Model)";

            var expectedHtml = @"<h1>Generic View Page</h1>

<h2>Form fields</h2>
<label for=""FirstName"">FirstName</label> <input id=""FirstName"" name=""FirstName"" type=""text"" value=""Demis"" />

<h2>Person Table</h2>
<table><caption>Demis's Links</caption><thead><tr><th>Name</th><th>Link</th></tr></thead>
<tbody>
<tr><td>ServiceStack</td><td>http://www.servicestack.net</td></tr><tr><td>AjaxStack</td><td>http://www.ajaxstack.com</td></tr></tbody>
</table>
".NormalizeNewLines();

            var dynamicPage = RazorFormat.CreatePage(template);

            var templateOutput = RazorFormat.RenderToHtml(dynamicPage, model: templateArgs).NormalizeNewLines();

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
Ejemplo n.º 2
0
        public void Can_Render_RazorPage_with_external_helper()
        {
            var template = @"<h1>View Page with Custom Helper</h1>

<h2>External Helper</h2>
<img src='path/to/img' class='inline-block' />
@Ext.InlineBlock(Model.FirstName, ""first-name"")
";

            var expectedHtml =
                @"<h1>View Page with Custom Helper</h1>

<h2>External Helper</h2>
<img src='path/to/img' class='inline-block' />
<div id=""first-name""><div class=""inner inline-block"">Demis</div></div>
".NormalizeNewLines();


            RazorFormat.PageBaseType = typeof(CustomViewBase <>);

            var dynamicPage = RazorFormat.CreatePage(template);

            var templateOutput = RazorFormat.RenderToHtml(dynamicPage, model: templateArgs).NormalizeNewLines();

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
Ejemplo n.º 3
0
        public void Can_Render_RazorPage_with_comments()
        {
            var template = @"<h1>Dynamic If Markdown Template</h1>

<p>Hello @Model.FirstName,</p>

@if (Model.FirstName == ""Bellot"") {
<ul>
	<li>@Model.FirstName</li>
</ul>
}
@*
@if (Model.LastName == ""Bellot"") {
	* @Model.LastName
}
*@

@*
Plain text in a comment
*@
<h3>heading 3</h3>";

            var expectedHtml = @"<h1>Dynamic If Markdown Template</h1>

<p>Hello Demis,</p>

<h3>heading 3</h3>".NormalizeNewLines();

            var dynamicPage = RazorFormat.CreatePage(template);

            var templateOutput = RazorFormat.RenderToHtml(dynamicPage, model: templateArgs).NormalizeNewLines();

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
        public void Nested_Layout_Example_With_Sections()
        {
            var websiteTemplate2 =
                @"<div id=""body2"">@RenderBody()</div>
                @RenderSection(""Section1"")"
                .StripLinesAndWhitespace();

            var websiteTemplate1 =
                @"@{Layout=""websiteTemplate2"";}
                <div id=""body1"">@RenderBody()</div>
                @RenderSection(""Section1"")
                @section Section1 {<div>Menu2</div>}"
                .StripLinesAndWhitespace();

            var pageTemplate =
                @"@{Layout=""websiteTemplate1"";}
                <h1>@DateTime.Now.Year</h1>
                @section Section1 {<div>Menu1</div>}"
                .StripLinesAndWhitespace();

            var expectedHtml = (@"<div id=""body2""><div id=""body1"">
                <h1>" + DateTime.Now.Year + @"</h1></div>
                <div>Menu1</div></div><div>Menu2</div>")
                               .StripLinesAndWhitespace();

            RazorFormat.AddFileAndPage("/views/websiteTemplate2.cshtml", websiteTemplate2);
            RazorFormat.AddFileAndPage("/views/websiteTemplate1.cshtml", websiteTemplate1);
            var dynamicPage = RazorFormat.AddFileAndPage(@"/page.cshtml", pageTemplate);
            var template    = RazorFormat.RenderToHtml(dynamicPage);

            Assert.That(template, Is.EqualTo(expectedHtml));
        }
Ejemplo n.º 5
0
        public void Can_Render_RazorPage_with_IF_statement()
        {
            var template = @"<h1>Dynamic If Markdown Template</h1>

<p>Hello @Model.FirstName,</p>

<ul>
@if (Model.FirstName == ""Bellot"") {
	<li>@Model.FirstName</li>
}
@if (Model.LastName == ""Bellot"") {
	<li>@Model.LastName</li>
}
</ul>

<h3>heading 3</h3>";

            var expectedHtml = @"<h1>Dynamic If Markdown Template</h1>

<p>Hello Demis,</p>

<ul>
	<li>Bellot</li>
</ul>

<h3>heading 3</h3>";

            var dynamicPage = RazorFormat.CreatePage(template);

            var templateOutput = RazorFormat.RenderToHtml(dynamicPage, model: templateArgs);

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
        public void Can_compile_simple_template_by_name()
        {
            const string template = "This is my sample template, Hello @Model.Name!";

            RazorFormat.AddFileAndPage("/simple.cshtml", template);
            var result = RazorFormat.RenderToHtml("/simple.cshtml", new { Name = "World" });

            Assert.That(result, Is.EqualTo("This is my sample template, Hello World!"));
        }
        public void Pages_with_errors_still_throw_exceptions_when_rendering()
        {
            const string template = "This is a bad template, Hello @SomeInvalidMember.Name!";

            RazorFormat.AddFileAndPage("/simple.cshtml", template);

            RazorFormat.RenderToHtml("/simple.cshtml", new { Name = "World" });
        }
Ejemplo n.º 8
0
        public void Can_Render_RazorPage_with_Nested_Statements()
        {
            var template = @"<h1>@Model.FirstName Dynamic Nested Markdown Template</h1>

<h1>heading 1</h1>

<ul>
@foreach (var link in Model.Links) {
	if (link.Name == ""AjaxStack"") {
	<li>@link.Name - @link.Href</li>
	}
}
</ul>

@if (Model.Links.Count == 2) {
<h2>Haz 2 links</h2>
<ul>
	@foreach (var link in Model.Links) {
		<li>@link.Name - @link.Href</li>
		foreach (var label in link.Labels) { 
			<li>@label</li>
		}
	}
</ul>
}

<h3>heading 3</h3>";

            var expectedHtml = @"<h1>Demis Dynamic Nested Markdown Template</h1>

<h1>heading 1</h1>

<ul>
	<li>AjaxStack - http://www.ajaxstack.com</li>
</ul>

<h2>Haz 2 links</h2>
<ul>
		<li>ServiceStack - http://www.servicestack.net</li>
			<li>REST</li>
			<li>JSON</li>
			<li>XML</li>
		<li>AjaxStack - http://www.ajaxstack.com</li>
			<li>HTML5</li>
			<li>AJAX</li>
			<li>SPA</li>
</ul>

<h3>heading 3</h3>";


            var dynamicPage = RazorFormat.CreatePage(template);

            var templateOutput = RazorFormat.RenderToHtml(dynamicPage, model: templateArgs);

            templateOutput.Print();
            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
Ejemplo n.º 9
0
        public void Can_Use_HtmlHelper_In_Page()
        {
            string pageSource = "@Html.TextBox(\"textBox\")";
            var    page       = RazorFormat.CreatePage(pageSource);

            var output = RazorFormat.RenderToHtml(page, model: templateArgs);

            Assert.That(output, Is.EqualTo(@"<input id=""textBox"" name=""textBox"" type=""text"" value="""" />"));
        }
Ejemplo n.º 10
0
        public void Can_Use_Model_Directive_With_HtmlHelper()
        {
            string pageSource = "@model " + typeof(Person).FullName + @"
@Html.TextBoxFor(a => a.FirstName)";

            var page   = RazorFormat.CreatePage(pageSource);
            var output = RazorFormat.RenderToHtml(page, model: templateArgs);

            Assert.That(output, Is.EqualTo(@"<input id=""FirstName"" name=""FirstName"" type=""text"" value=""Demis"" />"));
        }
Ejemplo n.º 11
0
        public void Can_Access_ViewData()
        {
            const string val        = "Hello";
            const string pageSource = @"@{ Html.ViewData[""X""] = """ + val + @"""; }
@Html.ViewData[""X""]
";

            var page   = RazorFormat.CreatePage(pageSource);
            var output = RazorFormat.RenderToHtml(page, model: templateArgs).Trim();

            Assert.That(output, Is.EqualTo(val));
        }
Ejemplo n.º 12
0
        public void Can_Access_ViewBag_From_Layout()
        {
            const string val        = "Hello";
            const string pageSource = @"@{ ViewBag.X = """ + val + @"""; }@ViewBag.X";

            var page = RazorFormat.CreatePage(pageSource);

            RazorFormat.AddFileAndPage(staticTemplatePath, @"<title>@ViewBag.X</title><body>@RenderBody()</body>");
            var output = RazorFormat.RenderToHtml(page, model: templateArgs).Trim();

            Assert.That(output, Is.EqualTo(@"<title>Hello</title><body>Hello</body>"));
        }
Ejemplo n.º 13
0
        public void Can_Render_RazorTemplate()
        {
            const string mockContents = "[Replaced with Template]";

            RazorFormat.AddFileAndPage(staticTemplatePath, staticTemplateContent);
            var page = RazorFormat.CreatePage(mockContents);

            var expectedHtml = staticTemplateContent.ReplaceFirst(RazorFormat.TemplatePlaceHolder, mockContents);

            var templateOutput = RazorFormat.RenderToHtml(page, model: templateArgs);

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
Ejemplo n.º 14
0
        public void Can_Render_RazorPage()
        {
            RazorFormat.AddFileAndPage(staticTemplatePath, staticTemplateContent);
            var dynamicPage = RazorFormat.AddFileAndPage(dynamicPagePath, dynamicPageContent);

            var expectedHtml = dynamicPageContent
                               .Replace("@Model.FirstName", person.FirstName)
                               .Replace("@Model.LastName", person.LastName);

            expectedHtml = staticTemplateContent.Replace(RazorFormat.TemplatePlaceHolder, expectedHtml);

            var templateOutput = RazorFormat.RenderToHtml(dynamicPage, model: templateArgs);

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
        public void Simple_static_example()
        {
            RazorFormat.Instance = null;
            var razor = new RazorFormat {
                VirtualPathProvider = new InMemoryVirtualPathProvider(new BasicAppHost()),
                EnableLiveReload    = false,
            }.Init();

            var page = razor.CreatePage("Hello @Model.Name! Welcome to Razor!");
            var html = razor.RenderToHtml(page, new { Name = "World" });

            html.Print();

            Assert.That(html, Is.EqualTo("Hello World! Welcome to Razor!"));
        }
Ejemplo n.º 16
0
        public void Simple_static_example()
        {
            var razor = new RazorFormat
            {
                //DefaultBaseType = typeof(CustomRazorBasePage<>), //Change custom base ViewPage
                VirtualPathProvider = new InMemoryVirtualPathProvider(new BasicAppHost()),
                TemplateProvider    = { CompileInParallelWithNoOfThreads = 0 },
            };

            razor.Init();

            razor.AddPage("Hello @Model.Name! Welcome to Razor!");
            var html = razor.RenderToHtml(new { Name = "World" });

            html.Print();

            Assert.That(html, Is.EqualTo("Hello World! Welcome to Razor!"));
        }
Ejemplo n.º 17
0
        public void Can_Render_RazorPage_with_foreach()
        {
            RazorFormat.AddFileAndPage(staticTemplatePath, staticTemplateContent);
            var dynamicPage = RazorFormat.AddFileAndPage(dynamicListPagePath, dynamicListPageContent);

            var expectedHtml = dynamicListPageContent
                               .Replace("@Model.FirstName", person.FirstName)
                               .Replace("@Model.LastName", person.LastName);

            var foreachLinks = "  <li>ServiceStack - http://www.servicestack.net</li>\r\n"
                               + "  <li>AjaxStack - http://www.ajaxstack.com</li>";

            expectedHtml = expectedHtml.ReplaceForeach(foreachLinks);

            expectedHtml = staticTemplateContent.Replace(RazorFormat.TemplatePlaceHolder, expectedHtml);

            var templateOutput = RazorFormat.RenderToHtml(dynamicPage, model: templateArgs);

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
Ejemplo n.º 18
0
        public void Can_inherit_from_Generic_RazorViewPage_from_model_directive()
        {
            var template = @"@model ServiceStack.ServiceHost.Tests.Formats_Razor.Person
<h1>Generic View Page</h1>

<h2>Form fields</h2>
@Html.LabelFor(m => m.FirstName) @Html.TextBoxFor(m => m.FirstName)
";

            var expectedHtml = @"<h1>Generic View Page</h1>

<h2>Form fields</h2>
<label for=""FirstName"">FirstName</label> <input id=""FirstName"" name=""FirstName"" type=""text"" value=""Demis"" />
".NormalizeNewLines();


            var dynamicPage = RazorFormat.CreatePage(template);

            var templateOutput = RazorFormat.RenderToHtml(dynamicPage, model: templateArgs).NormalizeNewLines();

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
        public void Simple_Layout_Example()
        {
            var websiteTemplate =
                @"<!DOCTYPE html>
<html>
    <head>
        <title>Simple Site</title>
    </head>
    <body>
    
        <div id=""header"">
            <a href=""/"">Home</a>
            <a href=""/About"">About</a>
        </div>
        
        <div id=""body"">
            @RenderBody()
        </div>
    
    </body>
</html>".NormalizeNewLines();

            var pageTemplate =
                @"@layout websiteTemplate

<h1>About this Site</h1>

<p>This is some content that will make up the ""about"" 
page of our web-site. We'll use this in conjunction
with a layout template. The content you are seeing here
comes from ^^^websiteTemplate.</p>

<p>And obviously I can have code in here too. Here is the
current date/year: @DateTime.Now.Year</p>
".NormalizeNewLines();

            var expectedHtml = @"<!DOCTYPE html>
<html>
    <head>
        <title>Simple Site</title>
    </head>
    <body>
    
        <div id=""header"">
            <a href=""/"">Home</a>
            <a href=""/About"">About</a>
        </div>
        
        <div id=""body"">
            <h1>About this Site</h1>

<p>This is some content that will make up the ""about"" 
page of our web-site. We'll use this in conjunction
with a layout template. The content you are seeing here
comes from ^^^websiteTemplate.</p>

<p>And obviously I can have code in here too. Here is the
current date/year: 2021</p>
        </div>
    
    </body>
</html>".NormalizeNewLines();


            RazorFormat.AddFileAndPage("/views/websiteTemplate.cshtml", websiteTemplate);
            var dynamicPage = RazorFormat.AddFileAndPage(@"/page.cshtml", pageTemplate);

            var template = RazorFormat.RenderToHtml(dynamicPage);

            Assert.That(template.NormalizeNewLines(), Is.EqualTo(expectedHtml));
        }
        public void Layout_MasterPage_Scenarios_Adding_Sections()
        {
            var websiteTemplate =
                @"<!DOCTYPE html>
<html>
    <head>
        <title>Simple Site</title>
    </head>
    <body>
    
        <div id=""header"">
            <a href=""/"">Home</a>
            <a href=""/About"">About</a>
        </div>
        
        <div id=""left-menu"">
            @RenderSection(""Menu"")
        </div>
        
        <div id=""body"">
            @RenderBody()
        </div>
        
        <div id=""footer"">
            @RenderSection(""Footer"")
        </div>
    
    </body>
</html>".NormalizeNewLines();

            var pageTemplate = @"<h1>About this Site</h1>

<p>This is some content that will make up the ""about"" 
page of our web-site. We'll use this in conjunction
with a layout template. The content you are seeing here
comes from ^^^websiteTemplate.</p>

<p>And obviously I can have code in here too. Here is the
current date/year: @DateTime.Now.Year</p>

@section Menu {
<ul>
  <li>About Item 1</li>
  <li>About Item 2</li>
</ul>
}

@section Footer {
<p>This is my custom footer for Home</p>
}
".NormalizeNewLines();

            var expectedHtml = @"<!DOCTYPE html>
<html>
    <head>
        <title>Simple Site</title>
    </head>
    <body>
    
        <div id=""header"">
            <a href=""/"">Home</a>
            <a href=""/About"">About</a>
        </div>
        
        <div id=""left-menu"">
            
<ul>
  <li>About Item 1</li>
  <li>About Item 2</li>
</ul>

        </div>
        
        <div id=""body"">
            <h1>About this Site</h1>

<p>This is some content that will make up the ""about"" 
page of our web-site. We'll use this in conjunction
with a layout template. The content you are seeing here
comes from ^^^websiteTemplate.</p>

<p>And obviously I can have code in here too. Here is the
current date/year: 2021</p>



        </div>
        
        <div id=""footer"">
            
<p>This is my custom footer for Home</p>

        </div>
    
    </body>
</html>".NormalizeNewLines();


            RazorFormat.AddFileAndPage("/views/websiteTemplate.cshtml", websiteTemplate);

            var dynamicPage = RazorFormat.AddFileAndPage("/page.cshtml", pageTemplate);

            var html = RazorFormat.RenderToHtml(dynamicPage, layout: "websiteTemplate");

            Assert.That(html, Is.EqualTo(expectedHtml));
        }
Ejemplo n.º 21
0
        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.PageBaseType = typeof(CustomViewBase <>);

            var websiteTemplatePath = "/views/websiteTemplate.cshtml";

            RazorFormat.AddFileAndPage(websiteTemplatePath, websiteTemplate);

            var staticPage = RazorFormat.CreatePage(template);

            var templateOutput = RazorFormat.RenderToHtml(staticPage, layout: "websiteTemplate").NormalizeNewLines();

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
Ejemplo n.º 22
0
        public void Can_Render_RazorTemplate_with_section_and_variable_placeholders()
        {
            var template            = @"<h2>Welcome to Razor!</h2>

@{ var lastName = Model.LastName; }

<p>Hello @Upper(lastName), @Model.FirstName,</p>

@section Breadcrumbs {
<h3>Breadcrumbs</h3>
@Combine("" / "", Model.FirstName, lastName)
}

@section Menus {
<h3>Menus</h3>
<ul>
@foreach (var link in Model.Links) {
	<li>@link.Name - @link.Href
		<ul>
		@{ var labels = link.Labels; }
		@foreach (var label in labels) { 
			<li>@label</li>
		}
		</ul>
	</li>
}
</ul>
}";
            var websiteTemplatePath = "websiteTemplate.cshtml";

            var websiteTemplate = @"<!doctype html>
<html lang=""en-us"">
<head>
	<title>Bellot page</title>
</head>
<body>

	<header>
		@RenderSection(""Menus"")
	</header>

	<h1>Website Template</h1>

	<div id=""content"">@RenderBody()</div>

	<footer>
		@RenderSection(""Breadcrumbs"")
	</footer>

</body>
</html>";

            var expectedHtml =
                @"<!doctype html>
<html lang=""en-us"">
<head>
	<title>Bellot page</title>
</head>
<body>

	<header>
		
<h3>Menus</h3>
<ul>
	<li>ServiceStack - http://www.servicestack.net
		<ul>

			<li>REST</li>
			<li>JSON</li>
			<li>XML</li>
		</ul>
	</li>
	<li>AjaxStack - http://www.ajaxstack.com
		<ul>

			<li>HTML5</li>
			<li>AJAX</li>
			<li>SPA</li>
		</ul>
	</li>
</ul>

	</header>

	<h1>Website Template</h1>

	<div id=""content""><h2>Welcome to Razor!</h2>



<p>Hello BELLOT, Demis,</p>


</div>

	<footer>
		
<h3>Breadcrumbs</h3>
Demis / Bellot

	</footer>

</body>
</html>".NormalizeNewLines();

            RazorFormat.PageBaseType = typeof(CustomViewBase <>);

            RazorFormat.AddFileAndPage("/views/{0}".Fmt(websiteTemplatePath), websiteTemplate);
            var page = RazorFormat.CreatePage(template);

            var result = RazorFormat.RenderToHtml(page, model: person, layout: websiteTemplatePath);

            var templateOutput = result.NormalizeNewLines();

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
Ejemplo n.º 23
0
        public void Can_capture_Section_statements_and_store_them_in_Sections()
        {
            var template = @"<h2>Welcome to Razor!</h2>

@{ var lastName = Model.LastName; }
@section Salutations {
<p>Hello @Upper(lastName), @Model.FirstName</p>
}

@section Breadcrumbs {
<h3>Breadcrumbs</h3>
<p>@Combine("" / "", Model.FirstName, lastName)</p>
}

@{ var links = Model.Links; }
@section Menus {
<h3>Menus</h3>
<ul>
@foreach (var link in links) {
	<li>@link.Name - @link.Href
		<ul>
		@{ var labels = link.Labels; }
		@foreach (var label in labels) { 
			<li>@label</li>
		}
		</ul>
	</li>
}
</ul>
}

<h2>Captured Sections</h2>

<div id='breadcrumbs'>
@RenderSection(""Breadcrumbs"")
</div>

@RenderSection(""Menus"")

<h2>Salutations</h2>
@RenderSection(""Salutations"")";

            var expectedHtml =
                @"<h2>Welcome to Razor!</h2>






<h2>Captured Sections</h2>

<div id='breadcrumbs'>

<h3>Breadcrumbs</h3>
<p>Demis / Bellot</p>

</div>


<h3>Menus</h3>
<ul>
	<li>ServiceStack - http://www.servicestack.net
		<ul>

			<li>REST</li>
			<li>JSON</li>
			<li>XML</li>
		</ul>
	</li>
	<li>AjaxStack - http://www.ajaxstack.com
		<ul>

			<li>HTML5</li>
			<li>AJAX</li>
			<li>SPA</li>
		</ul>
	</li>
</ul>


<h2>Salutations</h2>

<p>Hello BELLOT, Demis</p>
".NormalizeNewLines();

            RazorFormat.PageBaseType = typeof(CustomViewBase <>);

            var dynamicPage = RazorFormat.CreatePage(template);

            IRazorView razorView;
            var        templateOutput = RazorFormat.RenderToHtml(dynamicPage, out razorView, model: templateArgs).NormalizeNewLines();

            Assert.That(templateOutput.NormalizeNewLines(), Is.EqualTo(expectedHtml));

            var sectionHtml = razorView.RenderSectionToHtml("Salutations");

            Assert.That(sectionHtml.NormalizeNewLines(), Is.EqualTo("\n<p>Hello BELLOT, Demis</p>\n"));
        }
Ejemplo n.º 24
0
        public void Can_Render_RazorPage_with_variable_statements()
        {
            var template = @"<h2>Welcome to Razor!</h2>

@{ var lastName = Model.LastName; }
Hello @Upper(lastName), @Model.FirstName

<h3>Breadcrumbs</h3>
@Combine("" / "", Model.FirstName, lastName)

@{ var links = Model.Links; }
<h3>Menus</h3>
<ul>
@foreach (var link in links) {
	<li>@link.Name - @link.Href
		<ul>
		@{ var labels = link.Labels; }
		@foreach (var label in labels) { 
			<li>@label</li>
		}
		</ul>
	</li>
}
</ul>";

            var expectedHtml = @"<h2>Welcome to Razor!</h2>


Hello BELLOT, Demis

<h3>Breadcrumbs</h3>
Demis / Bellot


<h3>Menus</h3>
<ul>
	<li>ServiceStack - http://www.servicestack.net
		<ul>

			<li>REST</li>
			<li>JSON</li>
			<li>XML</li>
		</ul>
	</li>
	<li>AjaxStack - http://www.ajaxstack.com
		<ul>

			<li>HTML5</li>
			<li>AJAX</li>
			<li>SPA</li>
		</ul>
	</li>
</ul>".NormalizeNewLines();

            RazorFormat.PageBaseType = typeof(CustomViewBase <>);

            var dynamicPage = RazorFormat.CreatePage(template);

            var templateOutput = RazorFormat.RenderToHtml(dynamicPage, model: templateArgs).NormalizeNewLines();

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
Ejemplo n.º 25
0
        public void Can_Render_Razor_with_StaticMethods()
        {
            var headerTemplate = @"<h2>Header Links!</h2>
<ul>
	<li><a href=""http://google.com"">Google</a></li>
	<li><a href=""http://bing.com"">Bing</a></li>
</ul>".NormalizeNewLines();

            var template = @"<h2>Welcome to Razor!</h2>

@Html.Partial(""HeaderLinks"", Model)

<p>Hello @Upper(Model.LastName), @Model.FirstName</p>

<h3>Breadcrumbs</h3>

@Combine("" / "", Model.FirstName, Model.LastName)

<h3>Menus</h3>
<ul>
@foreach (var link in Model.Links) {
	<li>@link.Name - @link.Href
		<ul>
		@foreach (var label in link.Labels) { 
			<li>@label</li>
		}
		</ul>
	</li>
}
</ul>

<h3>HTML Table</h3>
@Table(Model)".NormalizeNewLines();

            var expectedHtml = @"<h2>Welcome to Razor!</h2>

<h2>Header Links!</h2>
<ul>
	<li><a href=""http://google.com"">Google</a></li>
	<li><a href=""http://bing.com"">Bing</a></li>
</ul>

<p>Hello BELLOT, Demis</p>

<h3>Breadcrumbs</h3>

Demis / Bellot

<h3>Menus</h3>
<ul>
	<li>ServiceStack - http://www.servicestack.net
		<ul>
			<li>REST</li>
			<li>JSON</li>
			<li>XML</li>
		</ul>
	</li>
	<li>AjaxStack - http://www.ajaxstack.com
		<ul>
			<li>HTML5</li>
			<li>AJAX</li>
			<li>SPA</li>
		</ul>
	</li>
</ul>

<h3>HTML Table</h3>
<table><caption>Demis's Links</caption><thead><tr><th>Name</th><th>Link</th></tr></thead>
<tbody>
<tr><td>ServiceStack</td><td>http://www.servicestack.net</td></tr><tr><td>AjaxStack</td><td>http://www.ajaxstack.com</td></tr></tbody>
</table>
".NormalizeNewLines();

            RazorFormat.PageBaseType = typeof(CustomViewBase <>);

            RazorFormat.AddFileAndPage("/views/HeaderLinks.cshtml", headerTemplate);

            var dynamicPage = RazorFormat.CreatePage(template);

            var templateOutput = RazorFormat.RenderToHtml(dynamicPage, model: templateArgs).NormalizeNewLines();

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }