一、語法 html
@{ 函數 int c=2; spa } orm @for(int i=0;i<c;i++) htm { blog @i string @:@i it @:select io @:select @i table select @i //@for 嵌套了,拋錯
}
select @c |
二、移除模板
關鍵是要建立配置
static ITemplateServiceConfiguration config = new TemplateServiceConfiguration();
public static void Init() { var service = RazorEngineService.Create(config); RazorEngine.Engine.Razor = service; }
/// <summary> /// 檢索多級模板,保留目錄結構 /// </summary> /// <param name="path"></param> public static List<string> LoadAllTemplate(string path) { path = path.TrimEnd('\\', '/'); List<string> list = new List<string>(); ; foreach (string s in new List<string>(System.IO.Directory.GetFiles(path, "*.t", System.IO.SearchOption.AllDirectories))) { //文件名後綴 string keyName = string.Format("{0}\\{1}", System.IO.Path.GetDirectoryName(s).Substring(path.Length + 1), System.IO.Path.GetFileNameWithoutExtension(s)).TrimStart('\\'); list.Add(keyName); var key = new NameOnlyTemplateKey(keyName, ResolveType.Global, null); string razorTemplate = System.IO.File.ReadAllText(s, Encoding.UTF8); RazorEngine.Engine.Razor.AddTemplate(key, new RazorEngine.Templating.LoadedTemplateSource(razorTemplate)); RazorEngine.Engine.Razor.Compile(key, typeof(RazorOption)); } return list; }
public static void Test() { LoadAllTemplate(System.IO.Path.GetFullPath("Templates")); RazorOption option = new RazorOption() { EMail = "1@1.1", NameSpace = "test", Registeredorganization = "bjzy" }; option.MetaString = "{Name:\"Test\"}"; //Console.WriteLine(option.ToMeta<Model.Table>()); //option.ObjectType = Enums.enumObjectType.Table; System.IO.TextWriter sw = new System.IO.StringWriter(); ITemplateKey key = RazorEngine.Engine.Razor.GetKey("ExportData\\Insert"); //Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(option, Newtonsoft.Json.Formatting.Indented)); RazorEngine.Engine.Razor.Run(key, sw, typeof(RazorOption), option); string fileName = System.Text.RegularExpressions.Regex.Match(sw.ToString(), @"(?<=\* 文件名:).*").Value.TrimEnd('\r', '\n'); (config.TemplateManager as DelegateTemplateManager).RemoveDynamic(key); RazorEngine.Engine.Razor.AddTemplate(key, "@DateTime.Now.ToString()"); RazorEngine.Engine.Razor.Compile(key, typeof(RazorOption)); RazorEngine.Engine.Razor.Run(key, sw, typeof(RazorOption), option); Console.Write(sw.ToString());
} |
三、嵌入方法或者函數
@helper 或 @function (貌似不支持)
參考 http://www.cnblogs.com/hbb0b0/p/5104027.html