如何在MVC使用JNTemplate模板引擎

翅膀的初衷

发表于2013-09-01 14:38:19

  在asp.net mvc中,System.Web.Mvc.VirtualPathProviderViewEngine实现了视图模板文件路径处理,我们只需要继承System.Web.Mvc.VirtualPathProviderViewEngine,实现CreatePartialView与CreateView方法便可实现自定义视图引擎,在此以国产开源模板引擎JNTemplaqte为例,演示怎么实现一个自定义的视图引擎!

  首先我们来实现JNTemplate视图,新建一个TemplateView.cs,代码如下:

   public class TemplateView : IView
   {
        private ITemplate templateManage = null;
        private string viewPath;

        public TemplateView(string path)
        {
            this.viewPath = path;
        }

        public TemplateView(ITemplate template)
        {
            this.templateManage = template;
        }

        public void Render(ViewContext viewContext, System.IO.TextWriter writer)
        {
            if (templateManage == null)
            {
                if (!string.IsNullOrEmpty(viewPath))
                {
                    if (viewPath.StartsWith("~/"))
                    {
                        viewPath = viewContext.HttpContext.Server.MapPath(viewPath);
                    }

                    templateManage = JNTemplate.BuildManager.CreateTemplate(viewPath);
                }
            }
            foreach (KeyValuePair<string, object> value in viewContext.ViewData)
            {
                this.templateManage.Context.TempData[value.Key] = value.Value;
            }

            templateManage.Render(writer);
        }
   }

 

  然后我们来自定义的我们的视图引擎,新建一个ViewEngine.cs,继承自System.Web.Mvc.VirtualPathProviderViewEngine 在这里我们只重载一下构造函数,不做其它实现:

    public abstract class ViewEngine : VirtualPathProviderViewEngine
    {
        public ViewEngine(string[] viewLocation, string[] masterLocation, string[] areaMasterLocation, string[] areaViewLocation)
            : this(viewLocation,masterLocation, areaMasterLocation, areaViewLocation, viewLocation, areaViewLocation)
        {
        }

        public ViewEngine(string[] viewLocation, string[] masterLocation, string[] areaMasterLocation, string[] areaViewLocation, string[] partialViewLocation, string[] areaPartialViewLocation)
        {
            base.MasterLocationFormats = masterLocation;
            base.AreaMasterLocationFormats = areaMasterLocation;
            base.ViewLocationFormats = viewLocation;
            base.AreaViewLocationFormats = areaViewLocation;
            base.PartialViewLocationFormats = partialViewLocation;
            base.AreaPartialViewLocationFormats = areaPartialViewLocation;
        }
    }

 

  然后再新建一个TemplateViewEngine.cs,继承自我们刚刚创建的ViewEngine:

    public class TemplateViewEngine: ViewEngine
    {

        public TemplateViewEngine(string[] viewLocation, string[] masterLocation, string[] areaMasterLocation, string[] areaViewLocation, string[] partialViewLocation, string[] areaPartialViewLocation):
            base(viewLocation,masterLocation,areaMasterLocation,areaViewLocation,partialViewLocation,areaPartialViewLocation)
        {
        }

        public TemplateViewEngine(string[] viewLocation)
            : this(viewLocation,viewLocation,viewLocation,viewLocation,viewLocation,viewLocation)
        {

        }

        public TemplateViewEngine():
            //默认搜索路径,参数0 为Action,1为Controller,2为Area
            this(new string[]{
                "~/View/{0}.html",
                "~/View/{0}.html",
                "~/View/{1}/{0}.html",
                "~/View/{1}/{0}.html",
            })
        {
        }

        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            return new TemplateView(partialPath);
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            return new TemplateView(viewPath);//new TemplateView(viewPath, masterPath);
        }
    }

然后打开网站根目录的Global.asax.cs(始果不存在则新建一个),在Application_Start 中添加:

ViewEngines.Engines.Add(new TemplateViewEngine());

 

至此,我们就可以正式使用JNTemplate模板了! 不过由于模板搜索路径已经被固化,无法实现在线动态换肤,如果要实现该功能,需要自己调整VirtualPathProviderViewEngine类,在下一次,我将讲解如何通过调整VirtualPathProviderViewEngine实现动态换肤功能