static void FastTemplateTest(Dictionary <Tree, Dictionary <Tree, Dictionary <Tree, List <Tree> > > > treeList) { TemplateEngine.Init(new HashSet <string>(), "/static/fastTemplate"); List <long> timeList = new List <long>(); long milliseconds = 0; Stopwatch watch = new Stopwatch(); for (int i = 0; i < 10000; i++) { watch.Reset(); watch.Start(); TemplateEngine.Compile("/static/fastTemplate/list.html", new Dictionary <string, dynamic> { { "list", treeList } }); watch.Stop(); milliseconds = watch.ElapsedMilliseconds; if (milliseconds == 0) { continue; } timeList.Add(milliseconds); } Console.WriteLine(string.Format("FastTemplateTest min:{0},max:{1} totalCount:10000 milliseconds > 0 count:{2}", timeList.Min(), timeList.Max(), timeList.Count)); }
private NHamlMonoRailView GetTemplate(IControllerContext controllerContext, IList <string> sources) { foreach (var abstractHelper in controllerContext.Helpers.Values) { TemplateEngine.Options.AddReferences(abstractHelper.GetType()); } var templateBuilderContext = new TemplateBuilderContext(); foreach (var key in controllerContext.Helpers.Keys) { templateBuilderContext.Helpers.Add((string)key, controllerContext.Helpers[key].GetType()); Debug.WriteLine(key); } var compiledTemplate = TemplateEngine.Compile(TemplateEngine.Options.TemplateBaseType, TemplateEngine.ConvertPathsToViewSources(sources), templateBuilderContext); var template = (NHamlMonoRailView)compiledTemplate.CreateInstance(); template.ViewEngine = this; var tempalteType = template.GetType(); foreach (var key in controllerContext.Helpers.Keys) { var property = tempalteType.GetField((string)key); var value = controllerContext.Helpers[key]; property.SetValue(template, value); } return(template); }
public static void Main() { string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\StoredProcedures.cst"); var engine = new TemplateEngine(new DefaultEngineHost(System.IO.Path.GetDirectoryName(path))); CompileTemplateResult result = engine.Compile(path); if (result.Errors.Count == 0) { var database = new DatabaseSchema(new SqlSchemaProvider(), @"Server=.;Database=PetShop;Integrated Security=True;"); TableSchema table = database.Tables["Inventory"]; CodeTemplate template = result.CreateTemplateInstance(); template.SetProperty("SourceTable", table); template.SetProperty("IncludeDrop", false); template.SetProperty("InsertPrefix", "Insert"); template.Render(Console.Out); } else { foreach (var error in result.Errors) { Console.Error.WriteLine(error.ToString()); } } Console.WriteLine("\r\nPress any key to continue."); Console.ReadKey(); }
static void ElseIfTest() { string content = TemplateEngine.Compile("/static/template/elseif.txt", new Dictionary <string, dynamic> { { "index", 10 } }); Console.WriteLine(string.Format("ElseIfTest:{0}", content)); Console.Read(); }
static void BaseTest() { string content = TemplateEngine.Compile("/static/template/base.txt", new Dictionary <string, dynamic> { { "name", "jack" } }); Console.WriteLine(string.Format("BaseTest:{0}", content)); Console.Read(); }
static void UsingTest() { string content = TemplateEngine.Compile("/static/template/using.txt", new Dictionary <string, dynamic> { { "time", DateTime.Now } }); Console.WriteLine(string.Format("UsingTest:{0}", content)); Console.Read(); }
static void LayoutTest() { string content = TemplateEngine.Compile("/static/template/index.html", new Dictionary <string, dynamic> { { "id", 1 }, { "cityName", "shanghai" } }); Console.WriteLine(string.Format("LayoutTest:{0}", content)); Console.Read(); }
protected Template CreateTemplate(params string[] templates) { var stopwatch = Stopwatch.StartNew(); var compiledTemplate = _templateEngine.Compile(templates); stopwatch.Stop(); Debug.WriteLine(string.Format("Compile took {0} ms", stopwatch.ElapsedMilliseconds)); return(compiledTemplate.CreateInstance()); }
static void EachTest() { string content = TemplateEngine.Compile("/static/template/each.txt", new Dictionary <string, dynamic> { { "title", "index" }, { "indexList", new List <int> { 1, 2, 3, 4 } } }); Console.WriteLine(string.Format("ElseIfTest:{0}", content)); Console.Read(); }
public void RenderTesting() { TemplateEngine engine = new TemplateEngine(System.IO.Path.GetDirectoryName(path)); CompileTemplateResult result = engine.Compile(path); if (!result.Errors.HasErrors) { CodeTemplate template = result.CreateTemplateInstance(); template.SetProperty("SampleStringProperty", "frank"); string render = template.RenderToString(); Assert.AreEqual("hello world frank", render); } }
static void IncludeTest() { string content = TemplateEngine.Compile("/static/template/include.txt", new Dictionary <string, dynamic> { { "name", "jack" }, { "list", new List <string> { "shanghai", "beijing" } } }); Console.WriteLine(string.Format("IncludeTest:{0}", content)); Console.Read(); }
/// <summary> /// This takes a filename and return an instance of the view ready to be used. /// If the file does not exist, an exception is raised /// The cache is checked to see if the file has already been compiled, and it had been /// a check is made to see that the compiled instance is newer then the file's modification date. /// If the file has not been compiled, or the version on disk is newer than the one in memory, a new /// version is compiled. /// Finally, an instance is created and returned /// </summary> public NHamlMonoRailView GetCompiledScriptInstance(string file, IControllerContext controllerContext) { // normalize filename - replace / or \ to the system path seperator var filename = file.Replace('/', Path.DirectorySeparatorChar) .Replace('\\', Path.DirectorySeparatorChar); filename = EnsurePathDoesNotStartWithDirectorySeparator(filename); Trace.WriteLine(string.Format("Getting compiled instance of {0}", filename)); GetTemplate(controllerContext, new [] { filename }); var template = TemplateEngine.Compile(file).CreateInstance(); return((NHamlMonoRailView)template); }
public ITemplate Compile <T>(string template) { if (Configuration.Configure.IsDevelopment) { _engine = new TemplateEngine(); } CompiledTemplate ct; if (!string.IsNullOrEmpty(Configuration.Configure.Views.Layout)) { ct = _engine.Compile(new List <string> { Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, Configuration.Configure.Views.Layout), Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, template) }, typeof(DataView <T>)); } else { ct = _engine.Compile(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, template), typeof(DataView <T>)); } return(new NHamlCompiledTemlpate(ct)); }
public static string GenerateString(string fullTemplateFileName, Dictionary <string, object> args) { string code = ""; DefaultEngineHost host = new DefaultEngineHost(Path.GetDirectoryName(fullTemplateFileName)); TemplateEngine engine = new TemplateEngine(System.IO.Path.GetDirectoryName(fullTemplateFileName)); CompileTemplateResult result = engine.Compile(fullTemplateFileName); if (!result.Errors.HasErrors) { CodeTemplate template = result.CreateTemplateInstance(); foreach (var key in args.Keys) { PropertyInfo info = template.GetPropertyInfo(key, false); object value = args[key]; if (info.PropertyType != value.GetType()) { string json = JsonHelper.Serialize(value); value = JsonHelper.Deserialize(json, info.PropertyType); } template.SetProperty(key, value); } code = template.RenderToString(); } else { StringBuilder sb = new StringBuilder(); foreach (var e in result.Errors) { if (e.IsError) { sb.AppendFormat("{0}\r\n", e.Description); } } if (sb.Length > 0) { throw new Exception(string.Format("编译模板时发生错误。\r\n{0}", sb.ToString())); } } return(code); }
public static void Main() { string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\StoredProcedures.cst"); var engine = new TemplateEngine(new DefaultEngineHost(System.IO.Path.GetDirectoryName(path))); CompileTemplateResult result = engine.Compile(path); if (result.Errors.Count == 0) { var database = new DatabaseSchema(new SqlSchemaProvider(), @"Server=.;Database=PetShop;Integrated Security=True;"); TableSchema table = database.Tables["Inventory"]; CodeTemplate template = result.CreateTemplateInstance(); template.SetProperty("SourceTable", table); template.SetProperty("IncludeDrop", false); template.SetProperty("InsertPrefix", "Insert"); template.Render(Console.Out); } else { foreach (var error in result.Errors) Console.Error.WriteLine(error.ToString()); } Console.WriteLine("\r\nPress any key to continue."); Console.ReadKey(); }
/// <summary> /// Create a view. /// </summary> /// <param name="context">Context to render</param> public void Render(IControllerContext context, IViewData viewData, TextWriter writer) { string layoutName; if (context.LayoutName != null) { layoutName = context.LayoutName; } else { var controllerName = context.ControllerUri.TrimEnd('/'); int pos = controllerName.LastIndexOf('/'); layoutName = context.ControllerUri; layoutName += pos == -1 ? controllerName + ".haml" : controllerName.Substring(pos + 1) + ".haml"; if (!MvcServer.CurrentMvc.ViewProvider.Exists(layoutName)) { layoutName = "Shared/Application.haml"; } } string viewPath = context.ViewPath + ".haml"; CompiledTemplate template = _templateEngine.Compile(new List <string> { layoutName, viewPath }, typeof(NHamlView)); var instance = (NHamlView)template.CreateInstance(); instance.ViewData = viewData; instance.Render(writer); }
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) { return((IView)_templateEngine.Compile( VirtualPathToPhysicalPath(controllerContext.RequestContext, partialPath), GetViewBaseType(controllerContext)).CreateInstance()); }