首頁  >  文章  >  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