public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext) { var template = renderContext.ViewCache.GetOrAdd(viewLocationResult, result => { try { var context = new NancyVeilContext(renderContext, Extensions); var engine = new VeilEngine(context); Type modelType = model == null ? typeof(object) : model.GetType(); return engine.CompileNonGeneric(viewLocationResult.Extension, result.Contents(), modelType); } catch (Exception e) { return CreateErrorPage(e); } }); var response = new HtmlResponse(); response.ContentType = "text/html; charset=utf-8"; response.Contents = s => { var writer = new StreamWriter(s, Encoding.UTF8); template(writer, model); writer.Flush(); }; return response; }
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext) { var template = renderContext.ViewCache.GetOrAdd(viewLocationResult, result => { try { var context = new NancyVeilContext(renderContext, Extensions); var engine = new VeilEngine(context); Type modelType = model == null ? typeof(object) : model.GetType(); return(engine.CompileNonGeneric(viewLocationResult.Extension, result.Contents(), modelType)); } catch (Exception e) { return(CreateErrorPage(e)); } }); var response = new HtmlResponse(); response.ContentType = "text/html; charset=utf-8"; response.Contents = s => { var writer = new StreamWriter(s, Encoding.UTF8); template(writer, model); writer.Flush(); }; return(response); }
public async Task <IView> CreateViewAsync(TemplateInfo templateInfo, Type modelType) { if (templateInfo == null) { return(null); } var hash = string.Concat("template_", templateInfo.Id, templateInfo.ETag, modelType.FullName); IView view; if (!_cacheProvider.TryGet(hash, out view)) { string content; using (var reader = new StreamReader(templateInfo.Open())) { content = await reader.ReadToEndAsync().ConfigureAwait(false); } var helperHandlers = _helperHandlerFactory.Create().ToArray(); var viewEngine = new VeilEngine(helperHandlers, _memberLocator); if (modelType == typeof(object)) { view = CreateNonGenericView(templateInfo.Id, content, viewEngine); } else { view = (IView)typeof(VeilViewEngine).GetMethod("CreateView", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(modelType).Invoke(null, new object[] { templateInfo.Id, content, viewEngine }); } _cacheProvider.Set(hash, view, DateTimeOffset.Now.AddHours(24)); } return(view); }
public VeilGCBenchmarkCase() { var templateContents = Templates.ReadTemplate("Template.sshtml"); var context = new VeilContext(); context.RegisterTemplate("Master", Templates.ReadTemplate("Master.sshtml")); context.RegisterTemplate("Roles", Templates.ReadTemplate("Roles.sshtml")); var engine = new VeilEngine(context); template = engine.Compile<ViewModel>("sshtml", new StringReader(templateContents)); }
private static string Generate(ContentModel model) { var engine = new VeilEngine(); var template = engine.Compile<ContentModel>("handlebars", new StringReader(Resources.Template)); using (var writer = new StringWriter()) { template(writer, model); return RemoveEmptyLines(writer.ToString()); } }
public VeilGCBenchmarkCase() { var templateContents = Templates.ReadTemplate("Template.sshtml"); var context = new VeilContext(); context.RegisterTemplate("Master", Templates.ReadTemplate("Master.sshtml")); context.RegisterTemplate("Roles", Templates.ReadTemplate("Roles.sshtml")); var engine = new VeilEngine(context); template = engine.Compile <ViewModel>("sshtml", new StringReader(templateContents)); }
public VeilHandlebarsRenderSpeedBenchmarkCase() { var templateContents = Templates.ReadTemplate("Template.hbs"); var context = new VeilContext(); context.RegisterTemplate("Roles", Templates.ReadTemplate("Roles.hbs")); var engine = new VeilEngine(context); compiledTypedTemplate = engine.Compile<ViewModel>("hbs", new StringReader(templateContents)); compiledUntypedTemplate = engine.CompileNonGeneric("hbs", new StringReader(templateContents), typeof(ViewModel)); compiledDynamicTemplate = engine.Compile<dynamic>("hbs", new StringReader(templateContents)); compiledDictionaryTemplate = engine.Compile<IDictionary<string, object>>("hbs", new StringReader(templateContents)); }
public VeilHandlebarsRenderSpeedBenchmarkCase() { var templateContents = Templates.ReadTemplate("Template.haml"); var context = new VeilContext(); context.RegisterTemplate("Roles", Templates.ReadTemplate("Roles.haml")); var engine = new VeilEngine(context); compiledTypedTemplate = engine.Compile <ViewModel>("haml", new StringReader(templateContents)); compiledUntypedTemplate = engine.CompileNonGeneric("haml", new StringReader(templateContents), typeof(ViewModel)); compiledDynamicTemplate = engine.Compile <dynamic>("haml", new StringReader(templateContents)); compiledDictionaryTemplate = engine.Compile <IDictionary <string, object> >("haml", new StringReader(templateContents)); }
private VeilView CompileView(string areaName, string controllerName, string viewName, Type modelType) { var viewFile = LocateView(areaName, controllerName, viewName); if (viewFile == null) { return null; } using (var contents = new StreamReader(viewFile.Open())) { var ctx = new MvcVeilContext(this, areaName, controllerName); var template = new VeilEngine(ctx).CompileNonGeneric( Path.GetExtension(viewFile.Name).TrimStart('.'), contents, modelType ); return new VeilView(template); } }
public void Should_work_with_no_veilcontext() { var view = new VeilEngine().Compile<ViewModel>("supersimple", new StringReader("Hello @Model.Name")); using (var writer = new StringWriter()) { view(writer, new ViewModel { Name = "Joe" }); Assert.That(writer.ToString(), Is.EqualTo("Hello Joe")); } }
public void Should_throw_if_attempt_to_use_partials_without_veilcontext() { Assert.Throws<InvalidOperationException>(() => { var view = new VeilEngine().Compile<ViewModel>("supersimple", new StringReader("Hello @Partial['Person'];")); }); }