Example #1
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);
        }
Example #2
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));
        }
Example #3
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);
        }