Example #1
0
 /// <summary>
 /// 替换模板的部分视图
 /// </summary>
 /// <param name="html"></param>
 /// <returns></returns>
 internal static string ReplacePartial(string html)
 {
     //如果包含部分视图,则替换成部分视图的内容
     if (partialRegex.IsMatch(html))
     {
         //替换模板里的部分视图,并将内容添加到模板内容中
         html = partialRegex.Replace(html, match =>
         {
             // 匹配的部分视图编号
             string matchValue = match.Groups[2].Value;
             string[] arr      = matchValue.Split('@');
             //Console.WriteLine("---" + arr[0]);
             if (partialIdRegexp.IsMatch(arr[0]))
             {
                 //Console.WriteLine("---" + arr[1]);
                 string content = TemplateUtility.ReadPartial(arr[0]);
                 if (content != null)
                 {
                     return(content);
                 }
             }
             return(String.Format("No such partial file \"{0}\"", matchValue));
         });
     }
     //返回替换部分视图后的内容
     return(html);
 }
Example #2
0
        /// <summary>
        /// 模板内容
        /// </summary>
        public string GetContent()
        {
            if (this._content == null)
            {
                FileInfo fi            = new FileInfo(this.FilePath);
                long     lastWriteUnix = TemplateUtility.Unix(fi.LastWriteTime);
                if (this._content == null || lastWriteUnix > this.LastModify)
                {
                    // 读取内容并缓存
                    StreamReader sr      = new StreamReader(this.FilePath);
                    string       content = sr.ReadToEnd();
                    sr.Dispose();
                    // 读取模板里的部分视图
                    content = TemplateRegexUtility.IncludeFileRegex.Replace(content, m =>
                    {
                        string path  = m.Groups[1].Value;
                        string tplId = TemplateUtility.GetPartialTemplateId(path, this.FilePath, out var partialFilePath);
                        return(m.Value.Replace(path, tplId + "@" + partialFilePath));
                    });
                    // 缓存内容
                    this.LastModify = lastWriteUnix;
                    this._content   = content;
                }

                // 替换系统标签
                this._content = TemplateRegexUtility.Replace(_content, m => TemplateCache.Tags[m.Groups[1].Value]);

                // 替换部分视图
                this._content = TemplateRegexUtility.ReplacePartial(this._content);
                // 处理解析事件
                if (this.OnResolve != null)
                {
                    this._content = this.OnResolve(this._content, this.Options);
                }
                // 压缩模板代码
                if (this.Options.EnabledCompress)
                {
                    this._content = TemplateUtils.CompressHtml(_content);
                }
                if (!this.Options.EnabledCache)
                {
                    var swp = this._content;
                    this._content = null;
                    return(swp);
                }
            }
            return(this._content);
        }
Example #3
0
 //递归方式注册模板
 private static void RegisterTemplates(DirectoryInfo dir, Options options)
 {
     foreach (FileInfo file in dir.GetFiles())
     {
         if (file.Extension.EndsWith(".html"))
         {
             // Console.WriteLine("---" + file.FullName);
             TemplateCache.RegisterTemplate(TemplateUtility.GetTemplateId(
                                                file.FullName, options.Names), file.FullName, options);
         }
     }
     foreach (DirectoryInfo dst in dir.GetDirectories())
     {
         //如果文件夹是可见的
         if ((dst.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
         {
             RegisterTemplates(dst, options);
         }
     }
 }
Example #4
0
        //递归方式注册模板
        private static void RegisterTemplates(DirectoryInfo dir, TemplateNames nameType)
        {
            // tml 为模板文件,防止可以被直接浏览
            Regex allowExt = new Regex("(.html|.part.html|.phtml)$", RegexOptions.IgnoreCase);

            foreach (FileInfo file in dir.GetFiles())
            {
                if (allowExt.IsMatch(file.Extension))
                {
                    // Console.WriteLine("---" + file.FullName);
                    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);
                }
            }
        }
Example #5
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));
        }