private void CompileTemplate(FreudeProject project) { string templateBody = ReadTemplateFile(); ICompiledRazorTemplate compiledTemplate = freudeTemplatingEngine.CompileTemplate("default", templateFileName, templateBody); project.RegisterTemplate("default", compiledTemplate); }
public string ExecuteWithLayout(ICompiledRazorTemplate innerTemplate, RazorEngineExecutionSettings executionSettings) { Contract.Requires(innerTemplate != null); Contract.Requires(executionSettings != null); Contract.Ensures(Contract.Result <string>() != null); throw new System.NotImplementedException(); }
public string ExpandTemplate(ICompiledRazorTemplate template, DocumentDef doc, string docHtml, FreudeProject project) { RazorEngineExecutionSettings executionSettings = new RazorEngineExecutionSettings(); executionSettings.Properties.Add("Doc", doc); executionSettings.Properties.Add("DocHtml", docHtml); executionSettings.Properties.Add("Project", project); return(template.Execute(executionSettings)); }
public void RegisterViewByModelInSubfolder() { SimulateViewFile("Sample", "admin"); engine.RegisterView <SampleViewModel>(null, "admin"); ICompiledRazorTemplate template = engine.GetViewTemplateByName("Sample"); Assert.IsNotNull(template); }
public void RegisterViewByNameInSubfolder() { SimulateViewFile("TestView", "admin"); engine.RegisterView("TestView", null, "admin"); ICompiledRazorTemplate template = engine.GetViewTemplateByName("TestView"); Assert.IsNotNull(template); }
string IFreudeTemplatingEngine.ExpandTemplate(ICompiledRazorTemplate template, DocumentDef doc, string docHtml, FreudeProject project) { Contract.Requires(template != null); Contract.Requires(doc != null); Contract.Requires(docHtml != null); Contract.Requires(project != null); Contract.Ensures(Contract.Result <string> () != null); throw new System.NotImplementedException(); }
public void EnsureSectionWithinSameTemplateIsRendered() { engine.RegisterView("Sample"); ICompiledRazorTemplate template = engine.GetViewTemplateByName("Sample"); RazorEngineExecutionSettings executionSettings = new RazorEngineExecutionSettings(); string result = template.Execute(executionSettings); //if (DebugMode) // Console.Out.WriteLine(result); StringAssert.Contains("<div>This is a sample section</div>", result); }
public void ViewResultShouldSupportOverridenStatusCode() { ICompiledRazorTemplate template = MockRepository.GenerateMock <ICompiledRazorTemplate>(); template.Expect(x => x.Execute(null)).IgnoreArguments().Return("rendered view"); viewRenderingEngine.Expect(x => x.GetViewTemplateByName("SomeModel")).Return(template); SampleViewModel model = new SampleViewModel(); ViewResult <SampleViewModel> result = new ViewResult <SampleViewModel>("SomeModel", model, razorEngineExecutionSettings); result.StatusCode = (int)HttpStatusCode.NotFound; result.Apply(context); Assert.AreEqual((int)HttpStatusCode.NotFound, context.StatusCode); }
public void RenderViewByName() { ICompiledRazorTemplate template = MockRepository.GenerateMock <ICompiledRazorTemplate>(); template.Expect(x => x.Execute(null)).IgnoreArguments().Return("rendered view"); viewRenderingEngine.Expect(x => x.GetViewTemplateByName("SomeModel")).Return(template); SampleViewModel model = new SampleViewModel(); ViewResult <SampleViewModel> result = new ViewResult <SampleViewModel>("SomeModel", model, razorEngineExecutionSettings); result.Apply(context); viewRenderingEngine.VerifyAllExpectations(); template.VerifyAllExpectations(); }
public void RegisterView <TModel>(string layoutName = null, string nspace = null) { ICompiledRazorTemplate layoutTemplate = null; if (layoutName != null) { if (!layoutTemplates.TryGetValue(layoutName, out layoutTemplate)) { throw new ArgumentException("Layout '{0}' not registered".Fmt(layoutName), "layoutName"); } } string viewName = ExtractViewName <TModel>(); RegisterViewPrivate(viewName, layoutTemplate, nspace); }
public void RegisterView(string viewName, string layoutName = null, string nspace = null) { if (log.IsDebugEnabled) { log.DebugFormat("Registering Razor view '{0}'", viewName); } ICompiledRazorTemplate layoutTemplate = null; if (layoutName != null) { if (!layoutTemplates.TryGetValue(layoutName, out layoutTemplate)) { throw new ArgumentException("Layout '{0}' not registered".Fmt(layoutName), "layoutName"); } } RegisterViewPrivate(viewName, layoutTemplate, nspace); }
public string ExecuteWithLayout(ICompiledRazorTemplate innerTemplate, RazorEngineExecutionSettings executionSettings) { if (LayoutTemplate != null) { throw new InvalidOperationException("Cannot execute this method on an inner template"); } object innerTemplateInstance = innerTemplate.CreateTemplateInstance(); SetTemplateProperties(executionSettings, innerTemplateInstance); ExecuteMethod(innerTemplateInstance); string innerBody = FetchTemplateOutput(innerTemplateInstance); object layoutTemplateInstance = CreateTemplateInstance(); SetLayoutInnerTemplate(layoutTemplateInstance, innerTemplateInstance, innerBody); ExecuteMethod(layoutTemplateInstance); return(FetchTemplateOutput(layoutTemplateInstance)); }
private void RegisterViewPrivate(string viewName, ICompiledRazorTemplate layoutTemplate, string nspace) { if (log.IsDebugEnabled) { log.DebugFormat("Registering Razor view '{0}'", viewName); } RazorEngineCompileSettings viewCompileSettings = compileSettings.Clone(); viewCompileSettings.DefaultClassName = viewName + compileSettings.DefaultClassName; string fileName; string containerDir; Contract.Assume(viewsRootDirectory != null); if (nspace == null) { containerDir = viewsRootDirectory; } else { containerDir = Path.Combine(viewsRootDirectory, nspace); } string viewContents = FetchViewContents(containerDir, viewName, out fileName); try { ICompiledRazorTemplate template = razorCompiler.Compile(viewContents, viewCompileSettings, viewName); template.LayoutTemplate = layoutTemplate; viewTemplates.Add(viewName, template); } catch (RazorException ex) { LogTemplateErrors(ex, fileName); throw; } }
public void RegisterLayout(string layoutName) { Contract.Assume(compileSettings != null); string fileName; string viewContents = FetchViewContents(viewsRootDirectory, layoutName, out fileName); if (log.IsDebugEnabled) { log.DebugFormat("Registering Razor layout '{0}'", layoutName); } try { ICompiledRazorTemplate template = razorCompiler.Compile(viewContents, compileSettings, layoutName); layoutTemplates.Add(layoutName, template); } catch (RazorException ex) { LogTemplateErrors(ex, fileName); throw; } }
public void TestCompilingOfAProject(bool withBuildDirDeletion) { ICompiledRazorTemplate compiledTemplate = MockRepository.GenerateMock <ICompiledRazorTemplate> (); projectBuilder.Stub(x => x.ListProjectFiles(null)) .IgnoreArguments().Return(new[] { @"projDir\weather.freude", @"projDir\site.css", @"projDir\content\other.css" }); fileSystem.Stub(x => x.ReadFileAsString(TemplateFileName)).Return(TemplateBody); fileSystem.Stub(x => x.ReadFileAsString(Path.Combine(ProjectDir, "weather.freude"))).Return(FreudeFileBody); templatingEngine.Stub(x => x.CompileTemplate(null, null, null)).IgnoreArguments().Return(compiledTemplate); templatingEngine.Stub(x => x.ExpandTemplate(null, null, null, null)).IgnoreArguments().Return(ExpandedBody); if (withBuildDirDeletion) { fileSystem.Stub(x => x.DoesFileExist(Path.Combine(BuildDir, BuildCommand.BuildMarkerFileName))) .Return(true); fileSystem.Expect(x => x.DeleteDirectory(BuildDir)).Repeat.Once(); } else { fileSystem.Expect(x => x.DeleteDirectory(BuildDir)).Repeat.Never(); } fileSystem.Expect(x => x.CopyFile(Path.Combine(ProjectDir, "site.css"), Path.Combine(BuildDir, "site.css"))); // a file in a subdirectory should also be copied fileSystem.Expect(x => x.CopyFile(Path.Combine(ProjectDir, @"content\other.css"), Path.Combine(BuildDir, @"content\other.css"))); // subdirectories staring with underscore should be ignored fileSystem.Expect(x => x.CopyFile(Path.Combine(ProjectDir, @"_templates\template.cshtml"), Path.Combine(BuildDir, @"_templates\template.cshtml"))) .Repeat.Never(); fileSystem.Expect(x => x.WriteFile(Path.Combine(BuildDir, "weather.html"), ExpandedBody, Encoding.UTF8)); fileSystem.Expect(x => x.WriteFile(Path.Combine(BuildDir, BuildCommand.BuildMarkerFileName), string.Empty, Encoding.UTF8)); cmd.ParseArgs(consoleEnv, new[] { ProjectDir, TemplateFileName, BuildDir, ".html" }); cmd.Execute(consoleEnv); fileSystem.VerifyAllExpectations(); }
public void RegisterTemplate(string templateId, ICompiledRazorTemplate template) { Contract.Requires(templateId != null); Contract.Requires(template != null); templates.Add(templateId, template); }