生成一页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中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号