当前位置:Gxlcms > html代码 > 深入了解html模板函数

深入了解html模板函数

时间:2021-07-01 10:21:17 帮助过:9人阅读

生成一页html的模板。其实最好用模板引擎,比如Razor之类。不过这个函数胜在方便。

  1. static private string HtmlTemplate(string body, string title, List<string> jsFiles, List<string> cssFiles)
  2. {
  3. string css = null;
  4. string js = null;
  5. if (body == null)
  6. {
  7. body = @"";
  8. }
  9. if (title == null)
  10. {
  11. title = @"";
  12. }
  13. if (jsFiles != null && jsFiles.Count > 0)
  14. {
  15. var sbjs = new StringBuilder();
  16. foreach(var file in jsFiles)
  17. {
  18. sbjs.Append(@"<script src=""").Append(file).Append(@"""></script>");
  19. }
  20. js = sbjs.ToString();
  21. }
  22. if (cssFiles != null && cssFiles.Count > 0)
  23. {
  24. var sbcss = new StringBuilder();
  25. foreach(var file in cssFiles)
  26. {
  27. sbcss.Append(@"<link href=""").Append(file).Append(@""" rel=""stylesheet"" type=""text/css"">");
  28. }
  29. css = sbcss.ToString();
  30. }
  31. var sb = new StringBuilder();
  32. sb.Append(@"<!DOCTYPE html><html><head>")
  33. .Append(@"<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">") // 显示中文
  34. .Append(@"<title>").Append(title).Append(@"</title>");
  35. if (!String.IsNullOrEmpty(js))
  36. {
  37. sb.Append(js);
  38. }
  39. if (!String.IsNullOrEmpty(css))
  40. {
  41. sb.Append(css);
  42. }
  43. sb.Append(@"</head><body>")
  44. .Append(@"<h1>").Append(title).Append(@"</h1>");
  45. sb.Append(body);
  46. sb.Append(@"</body></html>");
  47. return sb.ToString();
  48. }

以上就是深入了解html模板函数的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行