作为dtcms的使用者你是不是像我一样,也在不停的修改模板之后要点击生成模板浪费了很多开发模板的时间?

那就跟我一起给dtcms增加一个开发者模式,当模板修改完成之后,直接刷新页面就能看到效果,而不再重复的生成模板,生成模板了!

1,增加开发者模式控制选项,和开发是所用的模板

所需要修改的内容有dtcms Model层 sys_config.cs 增加两个字段

private int _develop = 0;
        private string _tempName = "main";

public int develop
        {
            get { return _develop; }
            set { _develop = value; }
        }
        public string tempName
        {
            get { return _tempName; }
            set { _tempName = value; }
        }

2,DTcms.Web   admin/settings/sys_config.aspx增加页面显示

<dl>
    <dt>开启开发模式</dt>
    <dd>
      <div class="rule-single-checkbox">
          <asp:CheckBox ID="develop" runat="server" />
      </div>
      <span class="Validform_checktip">*当选择开发模式时,请求访问链接自动生成当前请求页的模板!免去开发时要点击生成模板,使用此模式最好关闭伪静态</span>
    </dd>
  </dl>
  <dl>
    <dt>模板名称</dt>
    <dd>
      <asp:TextBox ID="tempName" runat="server" CssClass="input small" datatype="*1-100" sucmsg=" " />
      <span class="Validform_checktip">*当选择开发模式时,所使用的模板的文件夹名称</span>
    </dd>
  </dl>
  <dl>
3,DTcms.Web   admin/settings/sys_config.aspx.cs 增加以下红色部分代码

#region 赋值操作=================================
        private void ShowInfo()
        {
            BLL.siteconfig bll = new BLL.siteconfig();
            Model.siteconfig model = bll.loadConfig();
            webname.Text = model.webname;
            weburl.Text = model.weburl;
            webcompany.Text = model.webcompany;
            webaddress.Text = model.webaddress;
            webtel.Text = model.webtel;
            webfax.Text = model.webfax;
            webmail.Text = model.webmail;
            webcrod.Text = model.webcrod;
            webpath.Text = model.webpath;
            webmanagepath.Text = model.webmanagepath;
            staticstatus.SelectedValue = model.staticstatus.ToString();
            staticextension.Text = model.staticextension;
            develop.Checked = model.develop == 1;
            tempName.Text = model.tempName;
            if (model.memberstatus == 1)
---------------------------------------------------------------------------

/// <summary>
        /// 保存配置信息
        /// </summary>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("sys_config", DTEnums.ActionEnum.Edit.ToString()); //检查权限
            BLL.siteconfig bll = new BLL.siteconfig();
            Model.siteconfig model = bll.loadConfig();
            try
            {
                model.webname = webname.Text;
                model.weburl = weburl.Text;
                model.webcompany = webcompany.Text;
                model.webaddress = webaddress.Text;
                model.webtel = webtel.Text;
                model.webfax = webfax.Text;
                model.webmail = webmail.Text;
                model.webcrod = webcrod.Text;
                model.webpath = webpath.Text;
                model.webmanagepath = webmanagepath.Text;
                model.staticstatus = Utils.StrToInt(staticstatus.SelectedValue, 0);
                model.staticextension = staticextension.Text;
                model.develop = develop.Checked == true ? 1 : 0;
                model.tempName = tempName.Text;

4,DTcms.Web.UI HttpModule.cs 增加以下红色部分代码
/// <summary>
    /// DTcms的HttpModule类
    /// </summary>
    public class HttpModule : System.Web.IHttpModule
    {
        protected internal Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig();
        /// <summary>
        /// 实现接口的Init方法
        /// </summary>
        /// <param name="context"></param>
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(ReUrl_BeginRequest);
        }
        /// <summary>
        /// 实现接口的Dispose方法
        /// </summary>
        public void Dispose()
        { }
        #region 页面请求事件处理===================================
        /// <summary>
        /// 页面请求事件处理
        /// </summary>
        /// <param name="sender">事件的源</param>
        /// <param name="e">包含事件数据的 EventArgs</param>
        private void ReUrl_BeginRequest(object sender, EventArgs e)
        {
            HttpContext context = ((HttpApplication)sender).Context;
            Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig(); //获得站点配置信息
            string requestPath = context.Request.Path.ToLower(); //获得当前页面(含目录)
            //如果虚拟目录(不含安装目录)与站点根目录名相同则不需要重写
            if (IsDirExist(DTKeys.CACHE_SITE_DIRECTORY, siteConfig.webpath, siteConfig.webpath, requestPath))
            {
                return;
            }
            string requestDomain = context.Request.Url.Authority.ToLower(); //获得当前域名(含端口号)
            string sitePath = GetSitePath(siteConfig.webpath, requestPath, requestDomain); //获取当前站点目录
            string requestPage = CutStringPath(siteConfig.webpath, sitePath, requestPath); //截取除安装、站点目录部分
            //是否开启开发模式
            if (siteConfig.develop == 1)
            {
                string tempName=siteConfig.tempName;
                MarkTemplates(sitePath, tempName, requestPage);
            }

            //检查网站重写状态0表示不开启重写、1开启重写、2生成静态
            if (siteConfig.staticstatus == 0)
            {
                #region 站点不开启重写处理方法===========================
                //遍历URL字典,匹配URL页面部分
                foreach (Model.url_rewrite model in SiteUrls.GetUrls().Urls)
                {
                    //查找到与页面部分匹配的节点
                    if (model.page == requestPath.Substring(requestPath.LastIndexOf("/") + 1))
                    {
                        //如果该页面属于插件页则映射到插件目录,否则映射到站点目录
                        if (model.type == DTKeys.DIRECTORY_REWRITE_PLUGIN)
                        {
                            context.RewritePath(string.Format("{0}{1}/{2}{3}",
                                siteConfig.webpath, DTKeys.DIRECTORY_REWRITE_ASPX, DTKeys.DIRECTORY_REWRITE_PLUGIN, requestPage));
                            return;
                        }
                        else
                        {
                            context.RewritePath(string.Format("{0}{1}/{2}{3}",
                                siteConfig.webpath, DTKeys.DIRECTORY_REWRITE_ASPX, sitePath, requestPage));
                            return;
                        }
                    }
                }
                #endregion
            }
            else
            {
                #region 站点开启重写或静态处理方法=======================
                //遍历URL字典
                foreach (Model.url_rewrite model in SiteUrls.GetUrls().Urls)
                {
                    //如果没有重写表达式则不需要重写
                    if (model.url_rewrite_items.Count == 0 &&
                        Utils.GetUrlExtension(model.page, siteConfig.staticextension) == requestPath.Substring(requestPath.LastIndexOf("/") + 1))
                    {
                        //如果该页面属于插件页则映射到插件目录,否则映射到站点目录
                        if (model.type == DTKeys.DIRECTORY_REWRITE_PLUGIN)
                        {
                            context.RewritePath(string.Format("{0}{1}/{2}/{3}",
                                siteConfig.webpath, DTKeys.DIRECTORY_REWRITE_ASPX, DTKeys.DIRECTORY_REWRITE_PLUGIN, model.page));
                            return;
                        }
                        else
                        {
                            context.RewritePath(string.Format("{0}{1}/{2}/{3}",
                                siteConfig.webpath, DTKeys.DIRECTORY_REWRITE_ASPX, sitePath, model.page));
                            return;
                        }
                    }
                    //遍历URL字典的子节点
                    foreach (Model.url_rewrite_item item in model.url_rewrite_items)
                    {
                        string newPattern = Utils.GetUrlExtension(item.pattern, siteConfig.staticextension); //替换扩展名
                        //如果与URL节点匹配则重写
                        if (Regex.IsMatch(requestPage, string.Format("^/{0}$", newPattern), RegexOptions.None | RegexOptions.IgnoreCase)
                            || (model.page == "index.aspx" && Regex.IsMatch(requestPage, string.Format("^/{0}$", item.pattern), RegexOptions.None | RegexOptions.IgnoreCase)))
                        {
                            //如果开启生成静态、不是移动站点且是频道页或首页,则映射重写到HTML目录
                            if (siteConfig.staticstatus == 2 && !SiteDomains.GetSiteDomains().MobilePaths.Contains(sitePath) && 
                                (model.channel.Length > 0 || model.page.ToLower() == "index.aspx")) //频道页
                            {
                                context.RewritePath(siteConfig.webpath + DTKeys.DIRECTORY_REWRITE_HTML + "/" + sitePath +
                                        Utils.GetUrlExtension(requestPage, siteConfig.staticextension, true));
                                return;
                            }
                            else if (model.type == DTKeys.DIRECTORY_REWRITE_PLUGIN) //插件页
                            {
                                string queryString = Regex.Replace(requestPage, string.Format("/{0}", newPattern), item.querystring, RegexOptions.None | RegexOptions.IgnoreCase);
                                context.RewritePath(string.Format("{0}{1}/{2}/{3}", 
                                    siteConfig.webpath, DTKeys.DIRECTORY_REWRITE_ASPX, DTKeys.DIRECTORY_REWRITE_PLUGIN, model.page), string.Empty, queryString);
                                return;
                            }
                            else //其它
                            {
                                string queryString = Regex.Replace(requestPage, string.Format("/{0}", newPattern), item.querystring, RegexOptions.None | RegexOptions.IgnoreCase);
                                context.RewritePath(string.Format("{0}{1}/{2}/{3}",
                                    siteConfig.webpath, DTKeys.DIRECTORY_REWRITE_ASPX, sitePath, model.page), string.Empty, queryString);
                                return;
                            }
                        }
                    }
                }
                #endregion
            }
        }
        #endregion
        #region 辅助方法(私有)=====================================
        /// <summary>
        /// 获取URL的虚拟目录(除安装目录)
        /// </summary>
        /// <param name="webPath">网站安装目录</param>
        /// <param name="requestPath">当前页面,包含目录</param>
        /// <returns>String</returns>
        private string GetFirstPath(string webPath, string requestPath)
        {
            if (requestPath.StartsWith(webPath))
            {
                string tempStr = requestPath.Substring(webPath.Length);
                if (tempStr.IndexOf("/") > 0)
                {
                    return tempStr.Substring(0, tempStr.IndexOf("/")).ToLower();
                }
            }
            return string.Empty;
        }
        /// <summary>
        /// 获取当前域名包含的站点目录
        /// </summary>
        /// <param name="requestDomain">获取的域名(含端口号)</param>
        /// <returns>String</returns>
        private string GetCurrDomainPath(string requestDomain)
        {
            //当前域名是否存在于站点目录列表
            if (SiteDomains.GetSiteDomains().Paths.ContainsValue(requestDomain))
            {
                return SiteDomains.GetSiteDomains().Domains[requestDomain];
            }
            return string.Empty;
        }
        /// <summary>
        /// 获取当前页面包含的站点目录
        /// </summary>
        /// <param name="webPath">网站安装目录</param>
        /// <param name="requestPath">获取的页面,包含目录</param>
        /// <returns>String</returns>
        private string GetCurrPagePath(string webPath, string requestPath)
        {
            //获取URL的虚拟目录(除安装目录)
            string requestFirstPath = GetFirstPath(webPath, requestPath);
            if (requestFirstPath != string.Empty && SiteDomains.GetSiteDomains().Paths.ContainsKey(requestFirstPath))
            {
                return requestFirstPath;
            }
            return string.Empty;
        }
        /// <summary>
        /// 获取站点的目录
        /// </summary>
        /// <param name="webPath">网站安装目录</param>
        /// <param name="requestPath">获取的页面,包含目录</param>
        /// <param name="requestDomain">获取的域名(含端口号)</param>
        /// <returns>String</returns>
        private string GetSitePath(string webPath, string requestPath, string requestDomain)
        {
            //获取当前域名包含的站点目录
            string domainPath = GetCurrDomainPath(requestDomain);
            if (domainPath != string.Empty)
            {
                return domainPath;
            }
            // 获取当前页面包含的站点目录
            string pagePath = GetCurrPagePath(webPath, requestPath);
            if (pagePath != string.Empty)
            {
                return pagePath;
            }
            return SiteDomains.GetSiteDomains().DefaultPath;
        }
        /// <summary>
        /// 遍历指定路径目录,如果缓存存在则直接返回
        /// </summary>
        /// <param name="cacheKey">缓存KEY</param>
        /// <param name="dirPath">指定路径</param>
        /// <returns>ArrayList</returns>
        private ArrayList GetSiteDirs(string cacheKey, string dirPath)
        {
            ArrayList _cache = CacheHelper.Get<ArrayList>(cacheKey); //从续存中取
            if (_cache == null)
            {
                _cache = new ArrayList();
                DirectoryInfo dirInfo = new DirectoryInfo(Utils.GetMapPath(dirPath));
                foreach (DirectoryInfo dir in dirInfo.GetDirectories())
                {
                    _cache.Add(dir.Name.ToLower());
                }
                CacheHelper.Insert(cacheKey, _cache, 2); //存入续存,弹性2分钟
            }
            return _cache;
        }
        /// <summary>
        /// 遍历指定路径的子目录,检查是否匹配
        /// </summary>
        /// <param name="cacheKey">缓存KEY</param>
        /// <param name="webPath">网站安装目录,以“/”结尾</param>
        /// <param name="dirPath">指定的路径,以“/”结尾</param>
        /// <param name="requestPath">获取的URL全路径</param>
        /// <returns>布尔值</returns>
        private bool IsDirExist(string cacheKey, string webPath, string dirPath, string requestPath)
        {
            ArrayList list = GetSiteDirs(cacheKey, dirPath); //取得所有目录
            string requestFirstPath = string.Empty; //获得当前页面除站点安装目录的虚拟目录名称
            string tempStr = string.Empty; //临时变量
            if (requestPath.StartsWith(webPath))
            {
                tempStr = requestPath.Substring(webPath.Length);
                if (tempStr.IndexOf("/") > 0)
                {
                    requestFirstPath = tempStr.Substring(0, tempStr.IndexOf("/"));
                }
            }
            if (requestFirstPath.Length > 0 && list.Contains(requestFirstPath.ToLower()))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 截取安装目录和站点目录部分
        /// </summary>
        /// <param name="webPath">站点安装目录</param>
        /// <param name="sitePath">站点目录</param>
        /// <param name="requestPath">当前页面路径</param>
        /// <returns>String</returns>
        private string CutStringPath(string webPath, string sitePath, string requestPath)
        {
            if (requestPath.StartsWith(webPath))
            {
                requestPath = requestPath.Substring(webPath.Length);
            }
            sitePath += "/";
            if (requestPath.StartsWith(sitePath))
            {
                requestPath = requestPath.Substring(sitePath.Length);
            }
            return "/" + requestPath;
        }
        #endregion
        #region 生成模板=============================
        /// <summary>
        /// 生成模板
        /// </summary>
        private void MarkTemplates(string buildPath, string skinName,string requestPage)
        {
            //取得ASP目录下的所有文件
            string fullDirPath = Utils.GetMapPath(string.Format("{0}aspx/{1}/", siteConfig.webpath, buildPath));
            //获得URL配置列表
            BLL.url_rewrite bll = new BLL.url_rewrite();
            List<Model.url_rewrite> ls = bll.GetList("");
            DirectoryInfo dirFile = new DirectoryInfo(fullDirPath);
            //删除不属于URL映射表里的文件,防止冗余
            if (Directory.Exists(fullDirPath))
            {
                foreach (FileInfo file in dirFile.GetFiles())
                {
                    //检查文件
                    //Model.url_rewrite modelt = ls.Find(p => p.page.ToLower() == file.Name.ToLower());
                    //if (modelt == null)
                    //{
                    file.Delete();
                    //}
                }
            }
            //遍历URL配置列表
            foreach (Model.url_rewrite modelt in ls)
            {
                //如果URL配置对应的模板文件存在则生成
                string fullPath = Utils.GetMapPath(string.Format("{0}templates/{1}/{2}", siteConfig.webpath, skinName, modelt.templet));
                if (File.Exists(fullPath)&& requestPage.Contains(modelt.page.Replace(".html",".aspx")))
                {
                    PageTemplate.GetTemplate(siteConfig.webpath, "templates", skinName, modelt.templet, modelt.page, modelt.inherit, buildPath, modelt.channel, modelt.pagesize, 1);
                }
            }
        }
        #endregion
    }
ok 试试修改模板刷新页面看看是否能够自动生成模板了

给dtcms增加模板自动生成功能的更多相关文章

  1. 为hade增加model自动生成功能

    大家好,我是轩脉刃. 我们写业务的时候和db接触是少不了的,那么要生成model也是少不了的,如何自动生成model,想着要给hade框架增加个这样的命令. 看了下网上的几个开源项目,最终聚焦在两个项 ...

  2. T4 模板自动生成带注释的实体类文件

    T4 模板自动生成带注释的实体类文件 - 只需要一个 SqlSugar.dll 生成实体就是这么简单,只要建一个T4文件和 文件夹里面放一个DLL. 使用T4模板教程 步骤1 创建T4模板 如果你没有 ...

  3. Markdown 中的目录自动生成功能 TOC

    目录 Markdown 中的目录自动生成功能 TOC 1. 标题一 1.1 标题二 1.标题二 2. 标题一 2.1 标题二 2.2 标题二 Markdown 中的目录自动生成功能 TOC 1. 标题 ...

  4. Jtester+unitils+testng:DAO单元测试文件模板自动生成

    定位 本文适合于不愿意手工编写而想自动化生成DAO单元测试的筒鞋.成果是不能照搬的,但其中的"创建模板.填充内容.自动生成"思想是可以复用的.读完本文,可以了解 Python 读取 ...

  5. shell脚本模板----自动生成开头注释信息

    每当我们新建一个shell脚本都要去写一些繁琐的注释信息,这会浪费掉我们很多的时间,有没有感觉很痛苦呢? 哈哈 下面给大家分享一个shell脚本的模板文件,把它拷贝到用户的家目录下并命名成  .vim ...

  6. 导出 Excel 模板自动生成规则,避免用户来回修改

    一句话总结 Excel 导出.导入时,根据注解自动添加单元格验证规则,避免用户因填写错误的枚举字段而反复修改 Excel 需求背景 对于 Java Web 项目,总是不可避免的出现 Excel 导入. ...

  7. T4 模板自动生成带注释的实体类文件 - 只需要一个 SqlSugar.dll

    生成实体就是这么简单,只要建一个T4文件和 文件夹里面放一个DLL. 使用T4模板教程 步骤1 创建T4模板 ,一定要自已新建,把T4代码复制进去,好多人因为用我现成的T4报错(原因不明) 点击添加文 ...

  8. 写一个TT模板自动生成spring.net下面的配置文件。

    这个是目标. 然后想着就怎么开始 1.

  9. 【转】- 使用T4模板批量生成代码

    前言 之前在 “使用T4模板生成代码 - 初探” 文章简单的使用了T4模板的生成功能,但对于一个模板生成多个实例文件,如何实现这个方式呢?无意发现一个解决方案 “MultipleOutputHelpe ...

随机推荐

  1. js下 Day09、事件(二)

    一.事件流 事件流描述的是从页面中接收事件的顺序,目前主要有三个模型: #1. 事件冒泡: 事件开始时由最具体的元素接收,然后逐级向上传播到较为不具体的元素

  2. 多任务-python实现-使用生成器完成多任务(2.1.14)

    @ 目录 1.注意 2. 代码 关于作者 1.注意 先定义函数,函数里面放一个yiled 主函数生成该对象 执行while循环 调用生成器对象的next 因为每次调用next,不会继续执行,从而 完成 ...

  3. Tomcat文件包含漏洞的搭建与复现:CVE-2020-1938

    Tomcat文件包含漏洞的搭建与复现:CVE-2020-1938 漏洞描述 2020年2月20日,国家信息安全漏洞共享平台(CNVD)发布了Apache Tomcat文件包含漏洞(CNVD-2020- ...

  4. day020|python之面向对象基础2

    面向对象基础2 目录 面向对象基础2 7 对象与类型 7.1 类即类型 7.1.1 变量的三个指标 7.1.2 变量类型 7.2 list.append()方法原理 8 对象的高度整合 8.1 通过面 ...

  5. 干货满满,32个常用 Python 实现,你学废了嘛!

    1. 冒泡排序 lis = [56,12,1,8,354,10,100,34,56,7,23,456,234,-58] def sortport(): for i in range(len(lis)- ...

  6. 深入理解Spring Security授权机制原理

    原创/朱季谦 在Spring Security权限框架里,若要对后端http接口实现权限授权控制,有两种实现方式. 一.一种是基于注解方法级的鉴权,其中,注解方式又有@Secured和@PreAuth ...

  7. IaaS、PaaS、SaaS、DaaS都是什么?现在怎么样了?终于有人讲明白了

    导读:本文将详细科普云计算的概念.云服务的发展现状,并逐一介绍各种云服务模式(IaaS.PaaS.SaaS.DaaS),建议收藏! 作者:阿里云智能-全球技术服务部来源:大数据DT(ID:bigdat ...

  8. 远程桌面连接(出现身份验证错误。要求的函数不支持)这可能由于CredSSP加密Oracle修正。

    家庭版解决方案 在进行远程桌面时会遇到这种情况.对于Windows 10家庭版用户,是不支持组策略功能的.本文根据官方文档采用修改注册表的方式达到相同的目的. 1.打开注册表   win + R  键 ...

  9. 人脸识别--SeetaFace

    检测:http://download.csdn.net/detail/qq_14845119/9639840 对齐:http://download.csdn.net/detail/qq_1484511 ...

  10. Linux嵌入式学习-ds18b20驱动

    ds18b20的时序图如下: 复位时序: 读写时序: 以下是程序代码: #include <linux/module.h> #include <linux/init.h> #i ...