Ejemplo n.º 1
0
        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 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));
        }
        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" });
        }
        public void Pages_with_errors_dont_cause_exceptions_on_thread_starting_the_precompilation()
        {
            const string template = "This is a bad template, Hello @SomeInvalidMember.Name!";

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

            var page = RazorFormat.GetContentPage("/simple.cshtml");

            FuncUtils.WaitWhile(() => page.MarkedForCompilation || page.IsCompiling, millisecondTimeout: 5000);
            Assert.That(page.CompileException, Is.Not.Null);
        }
        public void New_pages_begin_compilation_when_added()
        {
            const string template = "This is my sample template, Hello @Model.Name!";

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

            var page = RazorFormat.GetContentPage("/simple.cshtml");

            FuncUtils.WaitWhile(() => page.MarkedForCompilation || page.IsCompiling, millisecondTimeout: 5000);
            Assert.That(page.IsValid);
        }
Ejemplo n.º 6
0
        public void OnBeforeEachTest()
        {
            RazorFormat.Instance = null;
            razorFormat          = new RazorFormat
            {
                VirtualPathProvider = new InMemoryVirtualPathProvider(new BasicAppHost()),
                PageBaseType        = typeof(CustomRazorBasePage <>),
                EnableLiveReload    = false,
            }.Init();

            razorFormat.AddFileAndPage("/views/TheLayout.cshtml", LayoutHtml);
        }
Ejemplo n.º 7
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.º 8
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.º 9
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));
        }
Ejemplo n.º 10
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));
        }
        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.º 13
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.º 14
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.º 15
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));
        }