.net c# 一个简单但是功能强大动态模板引擎
.net c# 一个简单但是功能强大动态模板引擎(一) 收藏
注意:欢迎转载,但是请注明出处.
流行的模板引擎有很多,譬如velocity.但是他的模板语言比较简,复杂的功能比较难实现,而且编辑模板比较麻烦容易出错.
但是利用UserControl就可以实现功能强大的一个动态模板引擎,编辑的模板的时候跟编辑一个用户控件没有区别,并且支持任何.net语言譬如c#.
下面就是代码:
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
namespace Template
{
public class TemplateBody : System.Web.UI.UserControl
{
private IDictionary<string, object> _context = new Dictionary<string, object>();
protected void Page_Load(object sender, EventArgs e)
{
}
public IDictionary<string, object> ViewData
{
get { return _context; }
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Template
{
public class TemplateBody : System.Web.UI.UserControl
{
private IDictionary<string, object> _context = new Dictionary<string, object>();
protected void Page_Load(object sender, EventArgs e)
{
}
public IDictionary<string, object> ViewData
{
get { return _context; }
}
}
}
TemplateBody类基本没什么代码只是声明了一个ViewData属性,该属性用于向模板添加数据由模板来获取并展示.
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web.UI;
namespace Template
{
public class TemplateEngine:IDisposable
{
private UserControl _uc;
private TemplateBody _tpl;
public TemplateEngine()
{
_uc = new UserControl();
}
/// <summary>
/// 加载一个模板
/// </summary>
/// <param name="path">这个路径为相对路径</param>
public void Load(string path)
{
_tpl = _uc.LoadControl(path) as TemplateBody;
if (_tpl == null)
{
throw (new ArgumentException(path));
}
}
/// <summary>
/// 控制展示
/// </summary>
/// <returns>返回生成的字符串</returns>
public string Render()
{
TextWriter tw = new StringWriter();
Render(tw);
return tw.ToString();
}
/// <summary>
/// 展示模板
/// </summary>
/// <param name="writer">TextWriter对象,可以传Response.Output</param>
public void Render(TextWriter writer)
{
HtmlTextWriter htw = new HtmlTextWriter(writer);
_tpl.RenderControl(htw);
}
/// <summary>
/// 增加一个显示数据的上下文
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
public void AddContext(string key, object obj)
{
_tpl.ViewData.Add(key, obj);
}
public object this[string key]
{
get{
object ret;
_tpl.ViewData.TryGetValue(key, out ret);
return ret;
}
set { AddContext(key, value); }
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
_uc.Dispose();
}
}
~TemplateEngine()
{
Dispose(false);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web.UI;
namespace Template
{
public class TemplateEngine:IDisposable
{
private UserControl _uc;
private TemplateBody _tpl;
public TemplateEngine()
{
_uc = new UserControl();
}
/// <summary>
/// 加载一个模板
/// </summary>
/// <param name="path">这个路径为相对路径</param>
public void Load(string path)
{
_tpl = _uc.LoadControl(path) as TemplateBody;
if (_tpl == null)
{
throw (new ArgumentException(path));
}
}
/// <summary>
/// 控制展示
/// </summary>
/// <returns>返回生成的字符串</returns>
public string Render()
{
TextWriter tw = new StringWriter();
Render(tw);
return tw.ToString();
}
/// <summary>
/// 展示模板
/// </summary>
/// <param name="writer">TextWriter对象,可以传Response.Output</param>
public void Render(TextWriter writer)
{
HtmlTextWriter htw = new HtmlTextWriter(writer);
_tpl.RenderControl(htw);
}
/// <summary>
/// 增加一个显示数据的上下文
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
public void AddContext(string key, object obj)
{
_tpl.ViewData.Add(key, obj);
}
public object this[string key]
{
get{
object ret;
_tpl.ViewData.TryGetValue(key, out ret);
return ret;
}
set { AddContext(key, value); }
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
_uc.Dispose();
}
}
~TemplateEngine()
{
Dispose(false);
}
}
}
TemplateEngine 是负责显示的类,核心代码也就是调用了RenderControl函数.
下面是具体使用:
1.创建一个web工程,注意其他工程可能不支持.
2.添加默认页面Default.aspx
3.根目录添加一个TemplateTest.ascx的模板文件 扩展名默认是ascx,如果需要更改别的扩展名的话需要在web.config里在compilation节点增加下列代码:
view plaincopy to clipboardprint?
<buildProviders>
<add extension=".view" type="System.Web.Compilation.UserControlBuildProvider"/>
</buildProviders>
<buildProviders>
<add extension=".view" type="System.Web.Compilation.UserControlBuildProvider"/>
</buildProviders>
4.直接运行就可以.
Default.aspx代码:
view plaincopy to clipboardprint?
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Template;
namespace TemplateDemo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TemplateEngine te = new TemplateEngine();
te.Load("TemplateTest.ascx");
te.AddContext("Text", "Super Man");
te.Render(Response.Output);
}
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Template;
namespace TemplateDemo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TemplateEngine te = new TemplateEngine();
te.Load("TemplateTest.ascx");
te.AddContext("Text", "Super Man");
te.Render(Response.Output);
}
}
}
TemplateTest.ascx代码:
view plaincopy to clipboardprint?
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TemplateBody.cs" Inherits="Template.TemplateBody" %>
<% for (int i = 0; i < 10; i++){
%>
<%=ViewData["Text"]%>
<% }%>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TemplateBody.cs" Inherits="Template.TemplateBody" %>
<% for (int i = 0; i < 10; i++){
%>
<%=ViewData["Text"]%>
<% }%>
记住,模板必须要加这个头:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TemplateBody.cs" Inherits="Template.TemplateBody" %>
下面是显示结果:
Super Man Super Man Super Man Super Man Super Man Super Man Super Man Super Man Super Man Super Man
需要改进的地方:
目前模板只支持相对路径,因为.net他对文件进行缓存处理,这样运行一次模板后即编译模板并进行缓存,如果文件被更改将重新编译,提高效率.
如果您需要从数据库或者从一个Stream里加载模板的话,需要重写VirtualPathProvide,并且重写判断模板被更改的函数CacheDependency,还有获取虚拟文件的函数GetFile, 这样很容易实现从任何地方获取模板.如果您有兴趣可以进行改进,完善.
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/baoaya/archive/2009/07/27/4384178.aspx