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(); }
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); } }
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); }