コード例 #1
0
        //递归方式注册模板
        private static void RegisterTemplates(DirectoryInfo dir, TemplateNames nametype)
        {
            Regex allowExt = new Regex("(.html|.phtml)$", RegexOptions.IgnoreCase);

            foreach (FileInfo file in dir.GetFiles())
            {
                if (allowExt.IsMatch(file.Extension))
                {
                    TemplateCache.RegisterTemplate(TemplateUtility.GetTemplateID(file.FullName, nametype), file.FullName);
                }
            }
            foreach (DirectoryInfo _dir in dir.GetDirectories())
            {
                //如果文件夹是可见的
                if ((_dir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
                {
                    RegisterTemplates(_dir, nametype);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// 替换模板的部分视图
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        internal static string ReplacePartial(string html)
        {
            //匹配的部分视图编号
            string matchValue;

            //如果包含部分视图,则替换成部分视图的内容
            if (partialRegex.IsMatch(html))
            {
                //替换模板里的部分视图,并将内容添加到模板内容中
                html = Regex.Replace(html, "\\${partial:\"(.+?)\"}", match =>
                {
                    matchValue = match.Groups[1].Value;
                    return(Regex.IsMatch(matchValue, "^[a-z0-9]+$", RegexOptions.IgnoreCase)
                        ? TemplateUtility.Read(match.Groups[1].Value)
                        : match.Value);
                });
            }

            //返回替换部分视图后的内容
            return(html);
        }
コード例 #3
0
ファイル: TemplateUtility.cs プロジェクト: w1146869587/devfw
        /// <summary>
        /// 显示模板信息到页面
        /// </summary>
        public static void PrintTemplatesInfo()
        {
            string        templateContent = Embed.SysTemplatePage;
            StringBuilder sb = new StringBuilder();

            sb.Append(@"<style type=""text/css"">
                          table{width:100%;background:#eee;margin:0 auto;line-height:25px;color:#222;cursor:pointer}
                          table td{background:white;padding:0 8px;}
                          table th{background:#006699;color:white;}
                          table tr.hover td{background:#222;color:white;}
                          table tr.even td{}
                        </style>
                        <script type=""text/javascript"">
                             function dynamicTable(table) {
                                if (table && table.nodeName === 'TABLE') {
                                    var rows = table.getElementsByTagName('tr');
                                    for (var i = 0; i < rows.length; i++) {
                                    if (i % 2 == 1) if (!rows[i].className) rows[i].className = 'even';
                                    rows[i].onmouseover = function () {
                                        this.className = this.className.indexOf('even') != -1 ? 'hover even' : 'hover';
                                    };
                                    rows[i].onmouseout = function () {
                                        this.className = this.className.indexOf('even') == -1 ? '' : 'even';
                                    };
                                 }
                               }
                            }</script>

                        <table cellspacing=""1"" id=""templates"">
                            <tr>
                                <th style=""width:50px;""></th><th style=""width:150px;"">模板编号</th><th style=""width:80px;"">模板类型</th><th>文件名</th><th>模板注释</th><th>文件路径</th></tr>
				<!--
                <tr><td colspan=""6"" align=""center"" style=""background:#c20000;color:white"">扩展名为“.phtml”表示为一个部分视图;部分视图只能使用ID命名</td></tr>
                -->"        );

            Template tpl;
            string   tplFileName, tplContent; //模板文件名,内容
            int      i = 0;

            foreach (string key in TemplateCache.templateDictionary.Keys)
            {
                tpl         = TemplateCache.templateDictionary[key];
                tplFileName =
                    new Regex("templates(/|\\\\)+#*(.+?)$", RegexOptions.IgnoreCase).Match(tpl.FilePath).Groups[2].Value
                    .Replace("\\", "/");
                tplContent = tpl.Content;

                sb.Append("<tr><td class=\"center\">").Append((++i).ToString()).Append("</td><td class=\"center\">")
                .Append(key.ToLower()).Append("</td><td class=\"center\">")
                .Append(

                    //RegexUtility.partialRegex.IsMatch(tplContent) && tplContent.IndexOf("<title>") != -1
                    !tpl.FilePath.EndsWith(".phtml")
                            ? "<span style=\"color:#333\">模板页面</span>"
                            : "<span style=\"color:#006699\">部分视图</span>")
                .Append("</td><td>/").Append(tplFileName).Append("</td><td>").Append(tpl.Comment)
                .Append("</td><td>").Append(tpl.FilePath).Append("</td></tr>");
            }

            sb.Append(@"<tr><td colspan=""6"" align=""center"" style=""background:#f0f0f0;color:#333"">
						部分视图扩展名为“.phtml”,可允许格式如:
						&nbsp;&nbsp;A:${partial:""inc/header.phtml""}
						&nbsp;&nbsp;B:${partial:""/tmpdir/inc/header.phtml""}
						&nbsp;&nbsp;C:${partial:""../../inc/header.phtml""}
						</td></tr>"                        );

            sb.Append(
                "</table><script type=\"text/javascript\">dynamicTable(document.getElementsByTagName('table')[0]);</script>");

            templateContent = TemplateRegexUtility.Replace(templateContent, match =>
            {
                switch (match.Groups[1].Value)
                {
                case "title":
                    return("模板信息");

                case "content":
                    return(sb.ToString());

                case "year":
                    return(DateTime.Now.Year.ToString());
                }
                return(String.Empty);
            });

            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            response.Write(TemplateUtility.CompressHtml(templateContent));
            response.End();
        }
コード例 #4
0
        /// <summary>
        /// 替换模板数据
        /// </summary>
        /// <param name="templateID"></param>
        /// <param name="eval"></param>
        /// <returns></returns>
        internal static string ReplaceTemplate(string templateID, MatchEvaluator eval)
        {
            string html = TemplateUtility.Read(templateID);

            return(TemplateRegexUtility.Replace(html, eval));
        }
コード例 #5
0
 /// <summary>
 /// 压缩后的字符
 /// </summary>
 /// <returns></returns>
 public string ToCompressedString()
 {
     return(TemplateUtility.CompressHtml(ToString()));
 }
コード例 #6
0
        public override string ToString()
        {
            DateTime dt = DateTime.Now;

            //指定了模板ID
            if (!String.IsNullOrEmpty(templateID))
            {
                //读取内容
                templateHtml = TemplateUtility.Read(templateID);

                //替换部分视图
                templateHtml = TemplateRegexUtility.ReplacePartial(templateHtml);
            }

            //HttpContext.Current.Response.Write("<br />1." + (DateTime.Now - dt).Milliseconds.ToString());
            //初始化之前发生
            this.PreInit(this.TemplateHandleObject, ref templateHtml);

            //如果参数不为空,则替换标签并返回内容
            if (this.data.Count != 0)
            {
                foreach (string key in this.data.Keys)
                {
                    templateHtml = TemplateRegexUtility.ReplaceHtml(templateHtml, key, this.data[key].ToString());
                }
            }

            //  HttpContext.Current.Response.Write("<br />2." + (DateTime.Now - dt).Milliseconds.ToString());
            //执行模板语法
            templateHtml = Eval.Complie(dc, templateHtml, this.TemplateHandleObject);


            //替换自定义变量
            IDictionary <string, object> defineVars = dc.GetDefineVariable();

            if (defineVars != null && defineVars.Count != 0)
            {
                foreach (string key in defineVars.Keys)
                {
                    if (defineVars[key] is Variable)
                    {
                        templateHtml = Eval.ResolveVariable(templateHtml, (Variable)defineVars[key]);
                    }
                    else
                    {
                        templateHtml = TemplateRegexUtility.ReplaceHtml(templateHtml, key,
                                                                        (defineVars[key] ?? "").ToString());
                    }
                }
            }

            // HttpContext.Current.Response.Write("<br />3." + (DateTime.Now - dt).Milliseconds.ToString());

            //解析实体的值
            //templateHtml = Eval.ExplanEntityProperties(dc,templateHtml);

            //呈现之前处理
            this.PreRender(this.TemplateHandleObject, ref templateHtml);

            // HttpContext.Current.Response.Write("<br />4."+(DateTime.Now - dt).Milliseconds.ToString());

            return(templateHtml);
        }