如我有namespace StringTemplateDemo.Controllers
{
[HandleError]
public class HomeController : Controller
{
public void Index()
{
string path=this.HttpContext.Server.MapPath("~/Template/IndexTemplate.htm");
string content = System.IO.File.ReadAllText(path,Encoding.GetEncoding("gb2312"));
StringTemplate st = new StringTemplate(content);
st.SetAttribute("title", "页面标题");
st.SetAttribute("body", "标题内容");
this.HttpContext.Response.Write(st.ToString());
}
}
}IndexTemplate.htm<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>$title$</title>
</head>
<body>
$body$
$page1$
</body>
</html>最后生成为<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>页面标题</title>
</head>
<body>
标题内容
</body>
</html>但这些都是对内容进行替换的,
如果是列表呢
如生成的HTML为<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>页面标题</title>
</head>
<body>
标题内容
<ul>
<li>1,张三</li>
<li>2,李四</li>
<li>3,五王</li>
</ul>
</body>
</html>那我代码namespace StringTemplateDemo.Controllers
{
[HandleError]
public class HomeController : Controller
{
public void Index()
{
string path=this.HttpContext.Server.MapPath("~/Template/IndexTemplate.htm");
string content = System.IO.File.ReadAllText(path,Encoding.GetEncoding("gb2312"));
StringTemplate st = new StringTemplate(content);
st.SetAttribute("title", "页面标题");
st.SetAttribute("body", "标题内容"); Dictionary<string, object> obj = new Dictionary<string, object>();
obj.Add("1", "张三");
obj.Add("2", "李四");
obj.Add("3", "五王");
//????这里要怎么写,那个模板文件又要怎么改呢?
this.HttpContext.Response.Write(st.ToString());
}
}
}
{
[HandleError]
public class HomeController : Controller
{
public void Index()
{
string path=this.HttpContext.Server.MapPath("~/Template/IndexTemplate.htm");
string content = System.IO.File.ReadAllText(path,Encoding.GetEncoding("gb2312"));
StringTemplate st = new StringTemplate(content);
st.SetAttribute("title", "页面标题");
st.SetAttribute("body", "标题内容");
this.HttpContext.Response.Write(st.ToString());
}
}
}IndexTemplate.htm<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>$title$</title>
</head>
<body>
$body$
$page1$
</body>
</html>最后生成为<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>页面标题</title>
</head>
<body>
标题内容
</body>
</html>但这些都是对内容进行替换的,
如果是列表呢
如生成的HTML为<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>页面标题</title>
</head>
<body>
标题内容
<ul>
<li>1,张三</li>
<li>2,李四</li>
<li>3,五王</li>
</ul>
</body>
</html>那我代码namespace StringTemplateDemo.Controllers
{
[HandleError]
public class HomeController : Controller
{
public void Index()
{
string path=this.HttpContext.Server.MapPath("~/Template/IndexTemplate.htm");
string content = System.IO.File.ReadAllText(path,Encoding.GetEncoding("gb2312"));
StringTemplate st = new StringTemplate(content);
st.SetAttribute("title", "页面标题");
st.SetAttribute("body", "标题内容"); Dictionary<string, object> obj = new Dictionary<string, object>();
obj.Add("1", "张三");
obj.Add("2", "李四");
obj.Add("3", "五王");
//????这里要怎么写,那个模板文件又要怎么改呢?
this.HttpContext.Response.Write(st.ToString());
}
}
}
列表的话,改写模板模板中要有循环的标记这样 将数据源 eg : DataTable 传递 到 模板生成类中,替换相应 即可
$rptstart
<li>
<$item>
</li>
$rptend
</ul>取到$rptstart与$end之间的值,做为模板,然后循环替换再放回去.