Home  >  Article  >  Web Front-end  >  Learn more about html template functions

Learn more about html template functions

零下一度
零下一度Original
2017-04-26 15:20:231515browse

Generate a template of one page of html. In fact, it is best to use a template engine, such as Razor. But this function is more convenient.

  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();
        }

The above is the detailed content of Learn more about html template functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn