时间:2021-07-01 10:21:17 帮助过:9人阅读
生成一页html的模板。其实最好用模板引擎,比如Razor之类。不过这个函数胜在方便。
- static private string HtmlTemplate(string body, string title, List<string> jsFiles, List<string> cssFiles)
- {
- string css = null;
- string js = null;
- if (body == null)
- {
- body = @"";
- }
- if (title == null)
- {
- title = @"";
- }
- if (jsFiles != null && jsFiles.Count > 0)
- {
- var sbjs = new StringBuilder();
- foreach(var file in jsFiles)
- {
- sbjs.Append(@"<script src=""").Append(file).Append(@"""></script>");
- }
- js = sbjs.ToString();
- }
- if (cssFiles != null && cssFiles.Count > 0)
- {
- var sbcss = new StringBuilder();
- foreach(var file in cssFiles)
- {
- sbcss.Append(@"<link href=""").Append(file).Append(@""" rel=""stylesheet"" type=""text/css"">");
- }
- css = sbcss.ToString();
- }
- var sb = new StringBuilder();
- sb.Append(@"<!DOCTYPE html><html><head>")
- .Append(@"<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">") // 显示中文
- .Append(@"<title>").Append(title).Append(@"</title>");
- if (!String.IsNullOrEmpty(js))
- {
- sb.Append(js);
- }
- if (!String.IsNullOrEmpty(css))
- {
- sb.Append(css);
- }
- sb.Append(@"</head><body>")
- .Append(@"<h1>").Append(title).Append(@"</h1>");
- sb.Append(body);
- sb.Append(@"</body></html>");
- return sb.ToString();
- }
以上就是深入了解html模板函数的详细内容,更多请关注Gxl网其它相关文章!