ASP.NET MVC 使用AderTemplate模板引擎进行视图显示

ASP.NET MVC 使用AderTemplateEngine模板引擎进行视图显示

 

1. 创建ArtTemplateViewLocator视图文件定位类,代码如下:

namespace ArtLib

{

class ArtTemplateViewLocator : ViewLocator

{

public ArtTemplateViewLocator()

{

base.ViewLocationFormats = new string[] { “~/Views/{1}/{0}.htm”,

“~/Views/{1}/{0}.html”,

“~/Views/Shared/{0}.htm”,

“~/Views/Shared/{0}.html”

};

base.MasterLocationFormats = new string[] { “” };

 

}

}

}

 

2. 创建VariableResolver类用于AdrTemplate模板视图解析,并注册自定义组件TagList,代码如下:

namespace ArtLib

{

public static class VariableResolver

{

public static object ArtTemplateResolve(string filePath,IDictionary viewData)

{

//初始化模板引擎

TemplateManager template1 = TemplateManager.FromFile(filePath);

//设置模板数据

template1.SetValue(“ViewData”, viewData);

//注册自定义的标签

template1.RegisterCustomTag(“list”, new TagList());

 

return template1.Process();

}

}

}

3. 创建ArtTemplateViewEngine类用于处理视图请求,并赋予ViewData给模板引擎,代码如下:

IViewLocator _viewLocator = null;

 

public IViewLocator ViewLocator

{

get

{

if (this._viewLocator == null)

{

this._viewLocator = new ArtTemplateViewLocator();

}

return this._viewLocator;

}

set

{

this._viewLocator = value;

}

}

 

public string TemplatePath { get; set; }

 

#region IViewEngine 成员

 

public void RenderView(ViewContext viewContext)

{

string viewLocation = ViewLocator.GetViewLocation(viewContext, viewContext.ViewName);

if (string.IsNullOrEmpty(viewLocation))

{

throw new InvalidOperationException(string.Format(“View {0} could not be found.”, viewContext.ViewName));

}

 

string viewPath = HttpContext.Current.Request.MapPath(viewLocation);

 

//以下为模板解析

 

string viewTemplate = VariableResolver.ArtTemplateResolve(viewPath, viewContext.ViewData) as string;

 

HttpContext.Current.Response.Write(viewTemplate);

 

}

 

#endregion

4. 创建ArtTemplateControllerFactory类,用于Golbal.asax进行视图绑定,代码如下:

namespace ArtLib

{

public class ArtTemplateControllerFactory : DefaultControllerFactory

{

protected override IController CreateController(RequestContext requestContext, string controllerName)

{

Controller controller = (Controller)base.CreateController(requestContext, controllerName);

controller.ViewEngine = new ArtTemplateViewEngine();//修改默认的视图引擎为我们刚才创建的视图引擎

return controller;

}

 

}

}

5. 创建自定义的模板标签类:TagList.cs用于显示数据列表,代码如下:

namespace ArtLib

{

public class TagList : ITagHandler

{

#region ITagHandler 成员

 

public void TagBeginProcess(TemplateManager manager, Ader.TemplateEngine.Parser.AST.Tag tag, ref bool processInnerElements, ref bool captureInnerContent)

{

processInnerElements = true;

captureInnerContent = true;

}

 

public void TagEndProcess(TemplateManager manager, Ader.TemplateEngine.Parser.AST.Tag tag, string innerContent)

{

Expression exp;

 

exp = tag.AttributeValue(“pageSize”);

if (exp == null)

throw new Exception(“Missing attribute: pageSize”);

 

string pageSize = manager.EvalExpression(exp).ToString();

 

exp = tag.AttributeValue(“currentPage”);

if (exp == null)

throw new Exception(“Missing attribute: currentPage”);

 

string currentPage = manager.EvalExpression(exp).ToString();

Business business = new BusinessLogic();

List<InformationSimple> list = new List<InformationSimple>();

if (string.IsNullOrEmpty(pageSize))

{

list = business.Select<InformationSimple>();

}

else

{

Page page = new Page();

page.PageSize = Convert.ToInt32(pageSize);

page.CurrentPage = Convert.ToInt32(currentPage);

list = business.SelectByPage<InformationSimple>(ref page);

}

String html = “”;

foreach (InformationSimple info in list)

{

html = html + “<tr><td>” + info.InfoTitle + “</td></tr>”;

}

html = “<table>” + html + “</table>”;

manager.WriteValue(html);

 

}

 

#endregion

}

6. 修改ASP.NET MVC的测试项目的Global.asax文件的Application_Start()方法,加入自定义控制器工厂类:ArtTemplateControllerFactory,代码如下:

protected void Application_Start()

{

ControllerBuilder.Current.SetControllerFactory(typeof(ArtTemplateControllerFactory));

RegisterRoutes(RouteTable.Routes);

 

}

7. 创建模板文件Views/Home/Index.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>

</head>

<body>

<ad:list pageSize=”10″ currentPage=”1″></ad:list>

#ViewData["Title"]#

</body>

</html>

posted @ 2010-09-28 17:09  mikel  阅读(669)  评论(0编辑  收藏  举报