首页  >  文章  >  web前端  >  深入了解html模板函数

深入了解html模板函数

零下一度
零下一度原创
2017-04-26 15:20:231477浏览

生成一页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模板函数的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn